HighTechTalks DotNet Forums  

binding datagrid to property hierarchy e.g. "Pet.Name"

Dotnet Framework (WinForms DataBinding) microsoft.public.dotnet.framework.windowsforms.databinding


Discuss binding datagrid to property hierarchy e.g. "Pet.Name" in the Dotnet Framework (WinForms DataBinding) forum.



Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old   
JerryShea
 
Posts: n/a

Default binding datagrid to property hierarchy e.g. "Pet.Name" - 08-02-2004 , 06:21 AM






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

Reply With Quote
  #2  
Old   
JerryShea
 
Posts: n/a

Default RE: binding datagrid to property hierarchy e.g. "Pet.Name" - 08-03-2004 , 11:59 PM






Dear Moderator: I though I was guaranteed a response within 48 hours?

"JerryShea" wrote:

Quote:
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

Reply With Quote
  #3  
Old   
AT
 
Posts: n/a

Default RE: binding datagrid to property hierarchy e.g. "Pet.Name" - 08-05-2004 , 04:37 AM



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.


Reply With Quote
  #4  
Old   
JerryShea
 
Posts: n/a

Default RE: binding datagrid to property hierarchy e.g. "Pet.Name" - 08-11-2004 , 01:55 AM



Thanks for that Jeffrey. The information you provided lead to the solution.
In the end I used some of the code from "bending...." in my list's impl. of
ITypedList and everything came together.

""Jeffrey Tan[MSFT]"" wrote:

Quote:
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.



Reply With Quote
  #5  
Old   
AT
 
Posts: n/a

Default RE: binding datagrid to property hierarchy e.g. "Pet.Name" - 08-11-2004 , 03:06 AM



Hi Jerry,

Thanks very much for your feedback.

I am glad my reply makes sense to you. Oh, yes, to enable the DataGrid call
your class's ICustomTypeDescriptor, you need implement a custom collection
class, which implement ITypedList class, it is similar as DataView class
and DataRowView class relation.

If you need further help, please feel free to tell me, I will help you.
Thanks

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.


Reply With Quote
  #6  
Old   
Manoj Mittal
 
Posts: n/a

Default Re: binding datagrid to property hierarchy e.g. "Pet.Name" - 09-01-2004 , 09:14 AM





Hi Jerry,
I am getting the same problem, i read your posts on message board can
you please guide me how you achieve the solution, i would be great if
you send me sample code...
Thanks,
Regards,
manoj

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Reply With Quote
Reply




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.