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.