views:

100

answers:

2

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?

A: 

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 a Control (the Grid) and returns an ICollection ` (of Fields).

drs9222
+1  A: 

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;
SchlaWiener
right, but this requires that everytime I remove a column from the datasource, if I miss removing it from the sort, I now have an exception when it can't find "colName". I was hoping for a more dynamic solution like an attribute tag I could put on the business object properties.
Maslow
accepting this answer as the easiest way, although I really wish there was a more strongly typed option.
Maslow