4
votes

I have a WPF datagrid with autogeneratecolumns = true. It is bound to a list of POCO Is there anyway I can set the column order by maybe setting some XML documentation on the properties in the class?

Obviously I can set autogenerate to false, and hard code the columns, but I'm wondering is there some other way of decorating my class/properties to take care of this.

1

1 Answers

2
votes

Not the nicest method, but works for me:

In the "AutoGeneratedColumns" event, you can set your columns DisplayIndex for each column by its header.

private void datagrid1_AutoGeneratedColumns(object sender, EventArgs e)
    {
        datagrid1_.Columns.FirstOrDefault(x => x.Header.ToString() == "header").DisplayIndex = 0;
    }

Take care of exceptions, etc...

Or I think the order of properties in the declaration of the class forecasts the autogenerated column order.