3
votes

I have a DataGridView object, the columns of which I am re-arranging by assigning them particular values for their DisplayIndex properties like so:

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

I am re-arranging the columns this way after I have populated the DGV object.

My question is this:

If I start with, for example, the grdMain.Columns["BarcodeNumber"] column being being the fifth column in the grid (index of 4), and then re-assign that column's DisplayIndex property to be 0, which of the following will correctly access the value of the BarcodeColumn:

grdMain.Columns[0]

or

grdMain.Columns[4]

Does altering the DisplayIndex for a DGV column affect its actual index on the grid?

Thanks!

2

2 Answers

4
votes

From MSDN:

The value of this property does not necessarily correspond to the current visual position of the band within the collection. For example, if the user reorders columns in a DataGridView at run time (assuming the AllowUserToOrderColumns property is set to true), the value of the Index property of each column will not change. Instead, the column DisplayIndex values change. Sorting rows, however, does change their Index values.

0
votes

I think it does though i'm not 100% sure as I would tend to use the column name

You would be better using the column name rather than the index unless you have a specific reason not to, this way you don't have to worry about the column index.