Array as a DataSource DataGrid: how to set up columns?

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

myDataGrid.DataSource = myArray;

This works, but I have a problem: it uses all properties as columns and uses property names as column headers. I cannot figure out how to configure two things:

  • Choose which subset of properties should be displayed in columns (for example, I have the ID, Name and Value property, I would like to show only the name and value);

  • Rename the column headings to make more sense (for example, if the property is called an identifier, displays the column heading labeled "Number").

Is this even possible?

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

+7
c # windows-mobile compact-framework datagrid
source share
3 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

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

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

+1
source share

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

In addition, there is the "DataGridView.AutoGenerateColumns" property, which you can set to false, so you do not need to bind all properties of your object. I also helped me help.

0
source share

In this code, _im is a table object, and I bind this object to the DataGridView dgvItem after binding. I am changing the dgvItem header text as needed.

  dgvItem.Rows.Clear(); dgvItem.DataSource = _im ; dgvItem.Columns[2].HeaderText = "Mobile Code"; dgvItem.Columns[3].HeaderText = "Mobile Name"; 
0
source share

All Articles