C# DataGridView Column Order
Is this a WinForms project or an Asp.net one?
If it's winforms you should be able to change the order the columns are displayed in by accessing your GridViews Columns DisplayIndex
dataGridView1.Columns["Park Name"].DisplayIndex = 0; // or 1, 2, 3 etc
My simple solution to the columns being out of order is to add this loop that sets the DisplayIndex
to the Index
.
foreach (DataGridViewColumn col in grid.Columns) {
col.DisplayIndex = col.Index;
}
The Index
is assigned to each column as they are added. I'm not sure why the DisplayIndex
becomes out of order, but the above script will fix it.
This might work as well as a one-liner:
grid.Columns.foreach(c => c.DisplayIndex = c.Index);
For me it did not the trick. One more line needed:
entityDataGridView.AutoGenerateColumns = false;
Regards!