问题描述
我有一个的DataGrid
链接到数据表
。
要这样的DataGrid我在后面的代码3列添加由我自己。
To this DataGrid I add by myself 3 columns in the code behind.
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
得到了我的数据表
为的ItemSource $ C 。$ C>
I wanted to re-index the columns of this DataGrid
who contains 19 columns but the problem is that when I do a count on my DataGrid
columns it count only the 3 columns added in the code behind even if my DataGrid
got my DataTable
as 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());
但在我的应用我改变,从数据表在的DataGrid
,现在我的列数返回31。
but when in my application I'm changing the filter that display the rows from DataTable
in the DataGrid
, now my column count returns 31.
任何人有什么我错过任何解释?由于debuging没什么小时卡梅斯出来了。
Anyone got any explanation about what I missed ? Because after hours of debuging nothing cames out.
推荐答案
的状态:
Automatically generated columns are not added to the Columns collection.
所以,这是一种正常的行为。
So this is a normal behaviour.
这篇关于DataGrid和列数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!