C# DataGridView列显示索引:它会改变列索引吗?

3

我有一个 DataGridView 对象,我通过为其 DisplayIndex 属性分配特定值来重新排列它的列,例如:

grdMain.Columns["BarcodeNumber"].DisplayIndex = 0;
grdMain.Columns["Type"].DisplayIndex = 1;
grdMain.Columns["Title"].DisplayIndex = 2;
grdMain.Columns["ParentID"].DisplayIndex = 3;

在填充了DGV对象之后,我正在以这种方式重新排列列

我的问题是:

例如,如果我从grdMain.Columns [“ BarcodeNumber”]列开始作为网格中的第五列(索引为4),然后重新分配该列的DisplayIndex属性为0,那么以下哪个将正确访问BarcodeColumn的值:

grdMain.Columns[0]

或者

grdMain.Columns[4]

更改DGV列的DisplayIndex是否会影响其在表格上的实际索引?
谢谢!
2个回答

4

MSDN网页上得知:

这个属性的值不一定对应于集合中该列的当前可视位置。例如,如果用户在运行时重新排序DataGridView中的列(假设AllowUserToOrderColumns属性设置为true),则每个列的Index属性值将不改变。相反,列DisplayIndex值会改变。但是,对行进行排序确实会更改它们的索引值。


0

我认为它确实可以,虽然我不是100%确定,因为我倾向于使用列名称。

除非您有特定原因,否则最好使用列名称而不是索引,这样您就不必担心列索引。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接