Datagrid: Binding to custom collection

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).
 
G

Guest

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);
}
 
C

Carlos Campos

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?
 
G

Guest

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top