Hi. I've been reading up about preparing objects for binding to Windows Forms
controls. However, I have not seen anything about how to get a control to
show an object with it's non value-type properties expanded.
For example consider the following. I want to bind a collection of
OuterObjects to a DataGrid, but the properties of the InnerObject property
are not expanded. All I get is the class name displayed as a column, wheras I
want to see InnerObject's "Name" property as a column. How do I do this
without creating a wrapper?
public class OuterObject
{
...
public InnerObject InnerObject
{
get{return innerObject;}
}
}
public class InnerObject
{
...
public string Name
{
get{return name;}
set{name = value;}
}
...
}
Thanks
kh
Hi,
Based on my understanding, you want to display the "deep" property of your
custom class in DataGrid Column.
Currently, .Net databinding still does not support deep databinding for
datasource. For your senario, the winform datagrid can only reflect the
first level properties of OuterObject class, then use these property type's
string representation for the first level property as column text.
For your requirement of display the deep property, the simplest way is
overriding InnerObject class's ToString() method, just return
InnerObject.Name property as the "InnerObject" type's string
representation. This is a little trick, which just change the type's string
representation logic to workaround this issue. But this trick will change
entire InnerObject class's string representation logic, which you may do
not want to do.
The solid solution for this issue is implement ICustomTypeDescriptor
interface for your OuterObject, this interface gives "OuterObject" a
description of the properties and enables you to create or remove or modify
the a class's properties at runtime. We can override
ICustomTypeDescriptor.GetProperties method, then find the "InnerObject"
property and return this inner "Name" property.
But for DataGrid to query the ICustomTypeDescriptor interface of
OuterObject class, we should implement a strong typed collection of
OuterObject, which implemented ITypedList interface. Then the DataGrid will
call ICustomTypeDescriptor interface when doing databinding.
For more information, please refer to my original post:
http://groups.google.com/groups?hl=zh-CN&lr=&c2coff=1&threadm=4K6Ioo3fEHA.33
40%40cpmsftngxa06.phx.gbl&rnum=2&prev=/groups%3Fq%3D%2BICustomTypeDescriptor
%2BDataGrid%2BITypedList%2B%2522Jeffrey%2522%26hl%3Dzh-CN%26lr%3D%26c2coff%3
D1%26selm%3D4K6Ioo3fEHA.3340%2540cpmsftngxa06.phx.gbl%26rnum%3D2
For example of how to implement ITypedList and ICustomTypeDescriptor,
please refer to the following article:
http://weblogs.asp.net/fbouma/articles/115837.aspx
http://www.codeproject.com/cs/database/itypedlist.asp?df=100&forumid=15239&e
xp=0&select=491147
Hope this helps
=======================================
Thank you for your patience and cooperation. If you have any questions or
concerns, please feel free to post it in the group. I am standing by to be
of assistance.
Best regards,
Jeffrey Tan
Microsoft Online Partner Support

Signature
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
kh - 03 Dec 2004 08:41 GMT
Jeffrey
Awesome. Thanks for your reply.
kh
> Hi,
>
[quoted text clipped - 49 lines]
> Get Secure! - www.microsoft.com/security
> This posting is provided "as is" with no warranties and confers no rights.
"Jeffrey Tan[MSFT]" - 06 Dec 2004 01:12 GMT
Hi,
You are welcome!! If you have any other questions, please feel free to
feedback. Thanks
Best regards,
Jeffrey Tan
Microsoft Online Partner Support

Signature
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.