all groups > dotnet windows forms databinding > april 2007 >
You're in the

dotnet windows forms databinding

group:

binding to nested objects


binding to nested objects koulbassa
4/17/2007 11:58:05 AM
dotnet windows forms databinding:
Can someone help me undestand how to do this properly?
here's the example class I want to bind to:

Public class Car
{
public string CarName;

public EngineData Engine;
public TireData Tires;
etc...
}

Public class EngineData
{
public string EngineName;
public string EngineSize;
etc...
}

Public Class TireData
{
public string TireBrand;
public string TreadPattern;
etc...
}

I want to bind my datagrid to a list of cars so that my grid will display
CarName, EngineName and TireBrand in their own columns. I set the datasource
as my list of cars and then set up the table style like the following:

DataGridTableStyle CarssTableStyle = new DataGridTableStyle();
DataGridColumnStyle cs = new DataGridTextBoxColumn();

// Create a DataGridColumn, set its header text and other properties
cs.MappingName = "CarName";
cs.HeaderText = "Car";
cs.Width = carNameColumnWidth;
ItemsTableStyle.GridColumnStyles.Add(cs);

// Create a DataGridColumn, set its header text and other properties
cs = new DataGridTextBoxColumn();
cs.MappingName = "Engine.EngineName";
cs.HeaderText = "Engine";
cs.Width = engineNameColumnWidth;
ItemsTableStyle.GridColumnStyles.Add(cs);

// Create a DataGridColumn, set its header text and other properties
cs = new DataGridTextBoxColumn();
cs.MappingName = "Tires.TireBrand";
cs.HeaderText = "Tires";
cs.Width = tireBrandColumnWidth;
ItemsTableStyle.GridColumnStyles.Add(cs);

The only column showing up on my grid is the CarName column. How would I
set up the binding for the other two columns without having to set up public
accessors in the car class for each of the properties I want to access in the
Re: binding to nested objects RobinS
4/17/2007 2:03:49 PM
..Net 2.0 or .Net 1.1?

Robin S.
-----------------------
[quoted text, click to view]

Re: binding to nested objects koulbassa
4/17/2007 2:40:01 PM
we're using 2.0.

[quoted text, click to view]
Re: binding to nested objects v-lliu NO[at]SPAM online.microsoft.com
4/18/2007 12:00:00 AM
Hi Koulbassa,

When we bind a DataGrid/DataGridView to a list, data binding will seek the
first-level properties in the object within the list and then display them
as columns in the DataGrid/DataGridView.

To bind a column to a nested property of an object, we need to modify the
metadata of the object. .NET Framework 1.x way is to implement
ICustomTypeDescriptor interface for the type of the object. When
implementing ICustomTypeDescriptor.GetProperties method, we could create
PropertyDescriptor instances for the second-level properties and return
them with the original PropertyDescriptor instances of the class.

..NET Framework 2.0 way is to make use of TypeDescriptionProvider and
CustomTypeDescriptor classes, which expand support for
ICustomTypeDescriptor.

Note that even if a type is added a TypeDesciptionProvider, data binding
won't call the custom type descriptor's GetProperties method to get the
object's properties. So we need to implement ITypedList interface for the
collection and in the ITypedList.GetItemProperties method, call
TypeDescriptor.GetProperties(type) which in turn calls the custom type
descriptor's GetProperties method.

The following is a sample of implementing ITypedList.

using System.Collections.Generic;
using System.ComponentModel;
class MyList<T>:List<T>,ITypedList
{
#region ITypedList Members

PropertyDescriptorCollection
ITypedList.GetItemProperties(PropertyDescriptor[] listAccessors)
{
return TypeDescriptor.GetProperties(typeof(T));
}

string ITypedList.GetListName(PropertyDescriptor[] listAccessors)
{
return "MyList";
}

#endregion
}

You may read my blog article named 'How to bind a DataGridView column to a
second-level property of a data source' for more information on how to
implement a custom type descriptor, via the following link:

http://blogs.msdn.com/msdnts/default.aspx

Hope this helps.
If you have any question, please feel free to let me know.

Sincerely,
Linda Liu
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
Re: binding to nested objects RobinS
4/18/2007 11:01:31 AM
Aside from Linda's suggestion, you can expose the properties of your nested
class and bind those directly. I've only done it as read-only, but I don't
know why it wouldn't work in both directions.

Public class Car
{
public string carName;
public EngineData Engine;

//expose the engine name from the Engine instance
public string EngineName
{
get {return Engine.EngineName;}
set {Engine.EngineName = value;}
}
//expose the engine size
public string EngineSize
{
get {return Engine.EngineSize;}
set {Engine.EngineSize = value;}
}
}

Public class EngineData
{
private string _EngineName;
public string EngineName
{
get {return _EngineName;}
set {_EngineName = value;}
}
private string _EngineSize;
public string EngineSize
{
get {return _EngineSize;}
set {_EngineSize = value;}
}
}

HTH,
Robin S.
--------------------------------------------------
[quoted text, click to view]

Re: binding to nested objects koulbassa
4/19/2007 8:58:06 AM
Hi All

Thanks for your reponses.

Given the complexity involved, I will likely use the solution given by
RobinS. We've only got a few objects...

Thanks again.

-Kevin

[quoted text, click to view]
AddThis Social Bookmark Button