views:

149

answers:

2

In my Windows Mobile .NET application I have a simple array of object with the data I want to display in my DataGrid. To do this, I simply call:

myDataGrid.DataSource = myArray;

This works, but I have a problem with it: it uses all properties as columns and uses the names of the properties as the column headers. I can't figure out how to customize two things:

  • Select which subset of properties should be displayed as columns (say I have an ID, Name and Value property, I'd only want to show Name and Value);

  • Rename the column headers to make more sense (for example if the property is called ID display a column header saying "Number").

Is this possible at all?

As mentioned this is in a Windows Mobile .NET (version 2) application.

+1  A: 

You need to set the Datagrid.TableStyles property to customize the layout.

http://msdn.microsoft.com/en-us/library/system.windows.forms.datagrid.tablestyles.aspx

More details on binding to an array of objects here: http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridtablestyle.mappingname(VS.71).aspx

To bind the System.Windows.Forms.DataGrid to a strongly typed array of objects, the object must contain public properties. To create a DataGridTableStyle that displays such an array, set the MappingName property to classname[] where classname is replaced by the class name. Also note that the MappingName property is case-sensitive.

code4life
I found these already before, but I can't quite figure how to they would work with an array as DataSource. The only information I can find is using it together with a DataTable.
pbean
Ah this works. A very good example is here: http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridtablestyle.mappingname.aspx
pbean
@pbean - glad to hear it works!
code4life
Thanks for this solution! :)A little addition: apparently I can edit the Table Styles in design time as well, by clicking the [...] button on the TableStyles property of the DataGrid. I need to fill in the correct type at the MappingName property (eg. "Person[]"). I can then edit the columns in the GridColumnStyles property (again with the [...] button), entering the property names (eg. "Surname") in its MappingName property, and the display name in the HeaderText property. Basically the same as in code, but design time it's a little bit easier. :)
pbean
A: 

I don't know if you know the name of the columns in advance? But if it's the case, you can go in the "Edit Columns" of your DataGridView and just create your columns there. In the "Data" Category, change the "DataPropertyName" from "(none)" to the name of the class property. From there you can customize the name, if it's visible, the size, etc. The DataGrid will bind it to your DataSource.

Also, there is a property "DataGridView.AutoGenerateColumns" that you can set to false so you don't have to bind all the properties of your object. I tought that migh help as well.

AngeDeLaMort
Unfortunately it's not about a DataGridView but about a DataGrid (the predecessor) and I'm unable to edit the columns in design time like I can with a DataGridView on a "normal" (Windows) Form.
pbean
So, you have to do it by hand via the tablestyles: http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridtablestyle(v=VS.100).aspxBut I suppose you could inherit from the DataGrid and make your own properties that map to the tableStyles so you can use the designer. But if you intent is to do that for one table, that might be not worth it.
AngeDeLaMort