2

我有一个DataGrid链接到一个DataTable.

在这个 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列进行计数时,即使我DataGrid得到了我DataTable的 as ,它也只计算后面代码中添加的 3 列ItemSource

 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());

但是当在我的应用程序中更改显示中的行的过滤器时DataTableDataGrid现在我的列数返回 31。

有人对我错过了什么有任何解释吗?因为经过数小时的调试,什么都没有出现。

4

1 回答 1

3

该文档指出:

Automatically generated columns are not added to the Columns collection.

所以这是一种正常的行为。

于 2013-07-05T08:06:40.693 回答