Question

Creating a custom business object to slide into a dataGridView works wonders... except I can't think of a programmatic way to set the column order.

Do I have to add them one by one by hand manually again just to get a nicer sort or is there some cryptic hidden attribute I can use?

Was it helpful?

Solution

Each dataGridViewColumn as a DisplayIndex Property which can be applied at any time and even works with AutoGenerateColumns set to true.

dataGridView1.Columns["colName"].DisplayIndex = 0;
dataGridView1.Columns["colAnotherColumn"].DisplayIndex = 1;

OTHER TIPS

I assume that you currently have AutoGenerateColumns set to true. The only way I know to control the order of the columns is to set AutoGenerateColumns to false and, like you said, manually add the columns.

EDIT:


I just looked into a vague memory of another way of doing this and think that you may want to look into the GridView control's ColumnsGenerator property which takes a IAutoFieldGenerator object. This interface has one method called GenerateFields' which takes aControl(theGrid) and returns an ICollection ` (of Fields).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top