2013-07-05 15 views
2

我有一個DataGrid鏈接到DataTableDatagrid和列數

對於這個DataGrid,我自己添加了3列代碼。

if (resultDataGrid.ItemsSource != null) 
      { 
       List<String> resColList = new List<String> { "Solde M.O", "Solde Deplacement", "Solde Pieces" }; 

       foreach (string cName in resColList) 
       { 
        DataGridTextColumn column = new DataGridTextColumn(); 
        column.Header = cName.ToUpper(); 
        column.Binding = new Binding(cName.ToUpper()); 
        resultDataGrid.Columns.Add(column); 
       } 
      } 

我想重新索引這個DataGrid誰包含19列,但問題是,當我做我的DataGrid列數指望它在代碼中添加哪怕只落後3列一列我DataGrid得到我的DataTableItemSource

if (_tableCase.Rows.Count > 0) 
      { 
       resultDataGrid.ItemsSource = _tableCase.AsDataView(); 
       createResultColumn(); 
       setColumnIndex(); //This is the fonction which set the new index. 
      } 
      else 
       resultDataGrid.ItemsSource = null; 

      MessageBox.Show(resultDataGrid.Columns.Count.ToString()); //this return 3 first run and then 31 if I change the filter. 

for (int i = 0; i < resultDataGrid.Columns.Count; ++i) // Only display the 3 column added in the code behind. 
       MessageBox.Show(resultDataGrid.Columns[i].Header.ToString()); 

,但是當我的申請,我改變,在DataGrid顯示從DataTable行,現在我的列數返回31

任何人有什麼我錯過任何解釋的過濾器?因爲經過幾個小時的調試,沒有任何東西出來。

回答

3

The documentation狀態:

Automatically generated columns are not added to the Columns collection. 

因此,這是一種正常的行爲。

+2

奇怪。你引用的文檔鏈接是'System.Web.UI.WebControls.DataGrid'的文檔,OP要求'System.Windows.Controls.DataGrid'中的wpf文件。在wpf列中添加到Columns集合中。你的回答非常錯誤。 –