Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Array as DataSource of a DataGrid: how to customize columns?

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.

like image 717
pbean Avatar asked Jun 12 '10 12:06

pbean


1 Answers

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.

like image 132
code4life Avatar answered Oct 20 '22 22:10

code4life