Datagrid: Binding to custom collection

  • Thread starter Thread starter Carlos Campos
  • Start date Start date
C

Carlos Campos

Hi,

I´m binding an array of my own class to a datagrid.

The problem I have is that I don´t know where I have to code the order of
the columns (the public properties of my class). Datagrid sort them in a
"weird" algorithm cause it does´t sort alphabetically or by ocurrence on the
class.

How can I tell the datagrid how to sort this columns (properties).
 
I believe the default order of the columns is the result of the order that
reflection returns the public properties.

To specify the order, you can create DataGridColumnStyle derived classes
(DataGridBoolColumn and DataGridTextBoxColumn) for each column and add them
to a DataGridTableStyle's GridColumnStyles collection. Then add that table
style to the grid.

Here's an example from the MSDN:

private void AddGridStyle()
{
DataGridTableStyle myGridStyle = new DataGridTableStyle();
myGridStyle.MappingName = "NamesTable";

DataGridTextBoxColumn nameColumnStyle =
new DataGridTextBoxColumn();
nameColumnStyle.MappingName = "Name";
nameColumnStyle.HeaderText= "Name";
myGridStyle.GridColumnStyles.Add(nameColumnStyle);

DataGridTimePickerColumn timePickerColumnStyle =
new DataGridTimePickerColumn();
timePickerColumnStyle.MappingName = "Date";
timePickerColumnStyle.HeaderText = "Date";
timePickerColumnStyle.Width = 100;
myGridStyle.GridColumnStyles.Add(timePickerColumnStyle);

grid.TableStyles.Add(myGridStyle);
}
 
I tried to use your code, but it didn´t work, maybe cause I don´t know what
I have to set in the MappingName. Checking Help the Mapping Name must
contain the DataTable Name, but I´m using an array of custom classes with
??? name, so I think the Datagrid is using the default style.

Any idea?
 
Carlos,

As I said in my e-mail to you, the MappingName should have the name of
the properties of the objects in your collection that you want displayed.

Pete
 
Back
Top