![]() | |
![]() |
| | Thread Tools | Search this Thread | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
I'd like to display top level properties and also second level properties in my datagrid. This is easier to explain with an example. Given these two classes: class Animal { public string Name { return "Rex"; } } class Person { public Animal Pet { return new Pet(); } public int Age { return 55; } } I'd like to bind my datagrid to an IList of Persons and display the Age property value in the 1st column and Pet.Name property value in the second column. If I create DataGridTextBoxColumns for Age or Pet it works OK but not for Pet.Name. Anyone got any ideas as to how to do this? Thanks in advance |
#3
| |||
| |||
|
#4
| |||
| |||
|
|
Hi Jerry, Normally, DataGrid will use reflection to query all the public property of the binding collection instance, it will not find deep in and bind the second level property. So what you want is not standarded supported. For this issue, I have 2 suggestions: 1. Modify your class difinition, for the Pet property, do not return entire Animal class, but return Animal.Name property. This is the simplest way. 2. For your Person class, implement ICustomTypeDescriptor interface, this interface enable you to create or remove or modify the a class's properties at runtime. You can override ICustomTypeDescriptor.GetProperties method, then find the Pet property and return this inner Name property. For a sample, please refer to "Bending the .NET PropertyGrid to Your Will": http://www.codeproject.com/cs/miscctrl/bending_property.asp There is a full implementation of ICustomTypeDescriptor in this article's source code. Althrough this article is with PropertyGrid, DataGrid control will also query ICustomTypeDescriptor interface for class instance. Also, the blow google link may help you: http://groups.google.com/groups?hl=zh-CN&lr=&ie=UTF-8&frame=right&th=7fee897 ce5ddb85f&seekm=OHrrtg8gDHA.2212%40tk2msftngp13.ph x.gbl ================================================== ========== Please apply my suggestion above and let me know if it helps resolve your problem. 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 Get Secure! - www.microsoft.com/security This posting is provided "as is" with no warranties and confers no rights. |
#5
| |||
| |||
|
#6
| |||
| |||
|
![]() |
| Thread Tools | Search this Thread |
| Display Modes | |
| |