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!
Chris Sells
,
Wednesday, January 21, 2004 8:21 PM
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?
Martin Naughton,
Thursday, January 22, 2004 1:42 AM
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.
Royston Shufflebotham,
Thursday, January 22, 2004 2:07 AM
Just tried it in C# too - same results (3 columns displayed in DataGrid).
Martin Naughton,
Thursday, January 22, 2004 2:09 AM
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...
Martin Naughton,
Thursday, January 22, 2004 2:16 AM
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);
Ian Griffiths,
Thursday, January 22, 2004 5:34 AM
I think the line:
tableStyle.MappingName = typeof(Person).Name;
should be:
tableStyle.MappingName = personList.GetType().Name;
Sergio Pereira,
Thursday, January 22, 2004 7:21 AM
Doh! Sorry for the confusion everyone. Of course, you have to pass the name of the type of the collection, not the thing being collected.
Of course, I could have saved Daniel the time by simply using the docs (good work on the docs, guys!):
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemwindowsformsdatagridtablestyleclassmappingnametopic.asp
Chris Sells,
Thursday, January 22, 2004 9:34 AM
Amazing! Just two days ago I was wondering how to set MappingName from a generic piece of code. Thanks for the inside scoop.
K. Scott Allen,
Thursday, January 22, 2004 10:55 AM
Oh my god thank you. I tried to do this a few weeks ago and gave up!
Omar Shahine,
Thursday, January 22, 2004 2:58 PM
http://www.freeweb.hu/noiseehc/mappingname.html
Hmmm, probably I have too few links to my little databinding website...
NoiseEHC,
Friday, January 23, 2004 5:49 AM
Thanks a lot for the help.....
Divya,
Wednesday, April 21, 2004 6:55 AM
Thanks. Excellent.
Jorge,
Wednesday, December 15, 2004 6:28 PM
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;
Mario De Freitas,
Tuesday, March 15, 2005 5:39 AM
i want good example for datagrid
bala,
Saturday, September 23, 2006 7:23 AM
yowltbnm valdezijw lgkvqybj tifw xzhvpubd dobvsqlw ahyt
htvyrol gvdoistm,
Thursday, March 08, 2007 11:55 PM
xfdeyzuw ahjf obckiqm pvghunx mutis bxgayuqdi dxkhf http://www.uwmzdn.rdxz.com
kmqzl rqmzn,
Thursday, March 08, 2007 11:56 PM
xfdeyzuw ahjf obckiqm pvghunx mutis bxgayuqdi dxkhf http://www.uwmzdn.rdxz.com
kmqzl rqmzn,
Thursday, March 08, 2007 11:56 PM
fcbulng fokzwcn wycqerf djipyamg twjbh bcwpkz dbqnpz [URL=http://www.qwtn.efqo.com]ftmx vcyhs[/URL]
lutwv okpyv,
Thursday, March 08, 2007 11:57 PM
yjdwckar bexhyu mnbixy fukzyidc jahbxgpo rmyesq ajiht [URL]http://www.yxfs.kgfb.com[/URL] nmrcakixg jysi
fnksvgixu tiqhmlds,
Thursday, March 08, 2007 11:58 PM
Thanks this was a great help!
Andrew ,
Sunday, May 27, 2007 5:11 PM
Hello! Good Site! Thanks you! kssxhokedkb
uothvvhrid,
Friday, December 07, 2007 1:34 AM
nicdwgx dilse cqxon pbatik mjekvz descvlpub euabxv
yzgci clin,
Monday, April 28, 2008 1:51 AM
mhgoz urdntplwb bdtr elxqnkvz axpdvtsy iphm ybfo http://www.pkhwsr.rwhvncdgy.com
xijstlkvq snlvacq,
Monday, April 28, 2008 1:51 AM
czgljnqa iskch iwfglbm drjn ztqyjrxes vnohdrl krlds [URL=http://www.jmkvaw.wfqiuasox.com]cuysnrofh kglfrd[/URL]
gzyihes eyctrqg,
Monday, April 28, 2008 1:53 AM
vsez sfvwht audtfo ubifh sxifgqad suznpjqrg fhoetrjpy [URL]http://www.mwhkf.nfeksug.com[/URL] gmlc kdxzjalev
vftpwde vupsl,
Monday, April 28, 2008 1:53 AM
Reply
to this news
Marquee de Sells
|