Wednesday, Jan 21, 2004, 8:21 PM
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!
13 comments
on this post
Martin Naughton:
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:
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:
Thursday, Jan 22, 2004, 2:09 AM
Martin Naughton:
tableStyle.MappingName = typeof(ArrayList).Name;
I changed the code and it works as hoped...
Thursday, Jan 22, 2004, 2:16 AM
Ian Griffiths:
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:
tableStyle.MappingName = typeof(Person).Name;
should be:
tableStyle.MappingName = personList.GetType().Name;
Thursday, Jan 22, 2004, 7:21 AM
K. Scott Allen:
Thursday, Jan 22, 2004, 10:55 AM
Omar Shahine:
Thursday, Jan 22, 2004, 2:58 PM
NoiseEHC:
Hmmm, probably I have too few links to my little databinding website...
Friday, Jan 23, 2004, 5:49 AM
Divya:
Wednesday, Apr 21, 2004, 6:55 AM
Jorge:
Wednesday, Dec 15, 2004, 6:28 PM
Mario De Freitas:
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:
Saturday, Sep 23, 2006, 7:23 AM




