Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > Datagrid ColumnStyle MappingName To SubClass of A Collection?
 

Datagrid ColumnStyle MappingName To SubClass of A Collection?

Hello:

I am binding a collection of strongly typed objects to a datagrid without a problem.  However, when I attempt to set the mapping name of a column to an object property within the base class the mapping does not take.  For instance: I have ClassA that has a property of the type ClassB with a property PropA that I want to map to.  I tried MappingName = "ClassB.PropA" but that doesn't seem to take.  At the worst case I could wrap a string return to that property off the base class and then map to that, but it just makes my classes ugly.  Any ideas?

Mike
MigrationUser 1  Wednesday, March 17, 2004 5:23 PM
Could you tell us a bit more about your collection. What is your class derived from, IBindingList, ITypedList, CollectionBase, .....?
MigrationUser 1  Sunday, March 21, 2004 10:34 PM
ArrayList, IBindingList, ITypedList

MigrationUser 1  Monday, March 22, 2004 11:17 AM
You might also like to use BindingManagerBase, BindingContext, and DataGridStyle, DataGridColumnStyle.


The MappingName property is usually set to the ColumnName of a DataColumn. Whenever the DataTable containing the DataColumn is displayed, the DataGridColumnStyle with the same MappingName will be used to display the data.

The comparison used to match the MappingName to the ColumnName is case-insensitive.

MigrationUser 1  Monday, March 22, 2004 7:23 PM
I am doing something very similar.
I found that you need to do the following:-

DataGridTableStyle myDataGridTableStyle = new DataGridTableStyle();
myDataGridTableStyle.MappingName = myDataSource.GetType().Name;
myDataGridTableStyle.GridColumnStyles.Clear();

DataGridTextBoxColumn myDataGridTextBoxColumnClientName = new DataGridTextBoxColumn();
myDataGridTextBoxColumnClientName.MappingName ="ClientName";
myDataGridTextBoxColumnClientName.HeaderText = "Client Name";
myDataGridTextBoxColumnClientName.Width = 140;
myDataGridTextBoxColumnClientName.TextBox.Enabled = false;
myDataGridTextBoxColumnClientName.NullText = " ";
myDataGridTableStyle.GridColumnStyles.Add(myDataGridTextBoxColumnClientName);

That seems to do the trick. The important part is 

myDataGridTableStyle.MappingName = myDataSource.GetType().Name;

Then, the mapping is directly between your property names 
myDataGridTextBoxColumnClientName.MappingName ="ClientName";

In this way, I can happily determine which columns of the datagrid map to the properties of my strongly typed collection
MigrationUser 1  Thursday, June 10, 2004 5:44 AM
I have a question related to this thread, so I thought I would post here.  I have a custom collection that I am binding to a datagrid, just like in the previous example. However, when I try to format the columns, it does not appear to recognize this.

i.e column.Format = "N2";   

Is there a method I have to overload in my classes??  
Thanks,
MigrationUser 1  Thursday, June 17, 2004 1:24 PM
You've given me some solid help but I'm sitll stumped.


I start out with:

daAdapter_A.Fill(dsDataSet.ABSMC_A);

In my formatting section I start with a:

grdTableStyle1.MappingName = dsDataSet.GetType().Name;

The value of MappingName is xsDataSet.

The rest is pretty simple colum format statements like:

grdColStyle1.HeaderText = "Location";
grdColStyle1.MappingName = "Loc";
grdColStyle1.Width = 200;


Ending with:

grdTableStyle1.GridColumnStyles.AddRange
(new DataGridColumnStyle[]
{grdColStyle1, grdColStyle2, grdColStyle3, grdColStyle4});

dataGrid1.TableStyles.Add(grdTableStyle1);

Nothing seems to work.

I'd appreciate any help that could be provided.

cb



MigrationUser 1  Saturday, June 26, 2004 4:54 PM
Hey, I had the same problem and just figured it out.

I'm going to spell out a full example so it makes sense for everyone.  I realize this isn't excatly what you have but the concept should be the same.  Here are 2 classes:

I'm just showing property names w/o code in this one for simplicity...

  Class CustomSubItem
       Property Name as String
       Property Color as Color
       Property ID as Integer
  End  Class

This one needs a little more code...

  Class MyItem
       Private mySubItems as ArrayList

       Sub New
           MyBase.New()
           mySubItems = New ArrayList
       End Sub

       Sub Add(byVal SubItem as CustomSubItem)
           SubItems.Add(SubItem)
       End Sub

       Property [Collection] as ArrayList
           Get
              Return mySubItems
           End Get
       End Property
  End Class

Basicly, you've created a custom Class to define your custom object.  You then have another custom class that contains as a property that is an ArrayList filled with your copies of your first custom class.

When you databind, you use your IList object (in this case the ArrayList but it could be a hashtable etc) as the datasource  (Object.DataSource=MyItem.Collection) of your table.

Now, when you go to create a TableStyle, the mapping name of the TableStyle itself is what is messing you up.  The TableStyle mapping name will be the Type name of the underlying IList object (in this case, the mapping name would be "ArrayList").  The column mapping names are then just their cooresponding properties (Name, Color, ID) on the custom object.

This one messed me up for a while this morning!!  lol  Hope this helps you out!
MigrationUser 1  Thursday, July 01, 2004 3:28 PM

You can use google to search for other answers

Custom Search

More Threads

• Failed to get the data source because of the following error: Cannot access a disposed object
• DataGridView maxes out at 140 rows
• PropertyGrid .NET 1.x vs. .NET 2.0
• Suppressing repeating values in rows with the DataGridView
• Problem DataGridView Validation
• Datagrid: multiple data elements in one cell
• what event is called after everything is finished with databinding?
• DataGridViewComboBoxCell - DisplayMember / ValueMember
• DataGridView SelectionMode FullRowSelect
• Popping Datagrid on another thread