Setting DataGrid Styles for Custom Types

I was data binding an ArrayList of objects of a custom type, e.g. class Person { public string Name; public DateTime BirthDate; public int Teeth; }, to a DataGrid today and everything working great 'til I wanted to filter one of the columns out (who cares how many Teeth a person has?). I was all set with table styles and grid styles like so:

void Form1_Load(object sender, EventArgs e) {
DataGridTableStyle tableStyle = new DataGridTableStyle();
tableStyle.DataGrid = this.dataGrid1;
tableStyle.MappingName = "Something???";
this.dataGrid1.TableStyles.Add(tableStyle);
  string[] columns = { "Name", "BirthDate" }; // Skip Teeth
foreach( string column in columns ) {
DataGridTextBoxColumn columnStyle = new DataGridTextBoxColumn();
columnStyle.HeaderText = column;
columnStyle.MappingName = column;
tableStyle.GridColumnStyles.Add(columnStyle);
}
  // Create ArrayList of Person objects...
  this.dataGrid1.DataSource = personList;
}

The problem was the table style's MappingName. When DataBinding to a DataTable, it's just the table name, but what is it when I've got a collection of objects of a custom type? Daniel Herling, a Software Design Engineer at Microsoft and the "man" of the DataGrid, came to my rescue:

If you bind to an ITypedList (say, System.Data.DataTable) then the mapping name should be ITypedList::GetListName(). For a System.Data.DataTable this would be the name of the data table.

If you don’t bind to an IList that is not ITypedList then the mapping name should be list.GetType().Name where list is the list the data grid is bound to.

So, all I had to do was set the table style mapping name to the type of my custom class:

  tableStyle.MappingName = typeof(ArrayList).Name;

This wasn't bad at all to bind a DataGrid to exactly the properties of my Person objects that I want w/o having to hack another custom shim type or changing the Person type itself to accommodate the data binding. Thanks, Daniel!



Comment Feed 13 comments on this post

Martin Naughton:


Hi,

Tried this sample in VB.NET - couldn't get it to work with an ArrayList of type Person.

Firstly, the DataGrid binding appears to require public Properties, as opposed to Fields. Therefore, I changed the Person class appropriately.

Secondly, the DataGrid displays all 3 properties, despite setting the configuration in code.

Any ideas?

Thursday, Jan 22, 2004, 1:42 AM


Royston Shufflebotham:


Here's a source I've found useful in the past on MappingName (and other DataGrid/DataBinding issues):

http://www.syncfusion.com/FAQ/WinForms/FAQ_c44c.asp#q931q

(It's item 5.71 of that page, in case the anchor doesn't work in your browser)

Cheers,
 Royston.

Thursday, Jan 22, 2004, 2:07 AM


Martin Naughton:


Just tried it in C# too - same results (3 columns displayed in DataGrid).

Thursday, Jan 22, 2004, 2:09 AM


Martin Naughton:


OK - I checked George Shepherd's page - his code suggests that the important line of code should be:

tableStyle.MappingName = typeof(ArrayList).Name;

I changed the code and it works as hoped...

Thursday, Jan 22, 2004, 2:16 AM


Ian Griffiths:


If all you want is the default table styles but with one column removed, I usually just let it create the table styles for me. The trick is pass the relevant currency manager as the constructor parameter. E.g.:

  cm = BindingContext[personList] as CurrencyManaged;
  DataGridTableStyle tableStyle = new DataGridTableStyle(cm);
  tableStyle.GridColumnStyles.Remove(tableStyle.ColumnGridStyles["Teeth"]);
  dataGrid1.TableStyles.Add(tableStyle);

Thursday, Jan 22, 2004, 5:34 AM


Sergio Pereira:


I think the line:
tableStyle.MappingName = typeof(Person).Name;

should be:
tableStyle.MappingName = personList.GetType().Name;

Thursday, Jan 22, 2004, 7:21 AM


K. Scott Allen:


Amazing! Just two days ago I was wondering how to set MappingName from a generic piece of code. Thanks for the inside scoop.

Thursday, Jan 22, 2004, 10:55 AM


Omar Shahine:


Oh my god thank you. I tried to do this a few weeks ago and gave up!

Thursday, Jan 22, 2004, 2:58 PM


NoiseEHC:


http://www.freeweb.hu/noiseehc/mappingname.html

Hmmm, probably I have too few links to my little databinding website...

Friday, Jan 23, 2004, 5:49 AM


Divya:


Thanks a lot for the help.....

Wednesday, Apr 21, 2004, 6:55 AM


Jorge:


Thanks. Excellent.

Wednesday, Dec 15, 2004, 6:28 PM


Mario De Freitas:


This is a good post, but I have found (like some others above) that trying to show only certain columns from a custom collection/array doesn't work when using the sample code provided in the MSDN etc...I think the problem lies somewhere with the propertydescriptor which needs to be set

Yes, you can use the example code above that shows you how to automatically create the TableStyle and then remove the columns you don't want...

The code below does the same, but enables you to add ONLY THE COLUMNS YOU DO want:

CurrencyManager cm = (CurrencyManager) BindingContext[orders];
ts = new DataGridTableStyle(cm);

dataGrid1.TableStyles.Add(ts);
ts.GridColumnStyles.Clear();
         

// Get the PropertyDescriptor for the DataColumn.
PropertyDescriptor pd = cm.GetItemProperties()["OrderNo"];
// Construct the DataGridColumnStyle with the PropertyDescriptor.

DataGridColumnStyle myColumn = new DataGridTextBoxColumn(pd);
myColumn.MappingName = "OrderNo";
myColumn.HeaderText = "Order Number";
dataGrid1.TableStyles[0].GridColumnStyles.Add(myColumn);


// Add the table style to the
//collection, but clear the
// collection first.
dataGrid1.TableStyles.Clear();
dataGrid1.TableStyles.Add(ts);

dataGrid1.DataSource = orders;

Tuesday, Mar 15, 2005, 5:39 AM


bala:


i want good example for datagrid

Saturday, Sep 23, 2006, 7:23 AM





comment on this post

HTML tags will be escaped.

Powered By ASP.NET

Hosted by SecureWebs

Microsoft

Mensa

IEEE


moving companies
addiction treatment
sunglasses
Kratom
How To Lose Weight Fast
cocktail dresses
Credit Card Balance Transfer
Add URL
Stock Trading
Health Insurance Quotes
Promotional Merchandise
Jet Privé
loans for bad credit