we're using 2.0.
> ..Net 2.0 or .Net 1.1?
>
[quoted text clipped - 63 lines]
> > the
> > classes Engine and Tires?
Linda Liu [MSFT] - 18 Apr 2007 06:09 GMT
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.
RobinS - 18 Apr 2007 19:01 GMT
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.
--------------------------------------------------
> we're using 2.0.
>
[quoted text clipped - 68 lines]
>> > the
>> > classes Engine and Tires?
koulbassa - 19 Apr 2007 16:58 GMT
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
> we're using 2.0.
>
[quoted text clipped - 65 lines]
> > > the
> > > classes Engine and Tires?