Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm Data Binding / April 2007

Tip: Looking for answers? Try searching our database.

binding to nested objects

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
koulbassa - 17 Apr 2007 19:58 GMT
Can someone help me undestand how to do this properly?
here's the example class I want to bind to:

Public class Car
{
    public string CarName;

     public EngineData Engine;
    public TireData Tires;
    etc...
}

Public class EngineData
{
    public string EngineName;
    public string EngineSize;
    etc...
}
   
Public Class TireData
{
    public string TireBrand;
    public string TreadPattern;
    etc...
}

I want to bind my datagrid to a list of cars so that my grid will display
CarName, EngineName and TireBrand in their own columns.  I set the datasource
as my list of cars and then set up the table style like the following:

    DataGridTableStyle CarssTableStyle = new DataGridTableStyle();
       DataGridColumnStyle cs = new DataGridTextBoxColumn();

       // Create a DataGridColumn, set its header text and other properties
       cs.MappingName = "CarName";
       cs.HeaderText = "Car";
       cs.Width = carNameColumnWidth;  
       ItemsTableStyle.GridColumnStyles.Add(cs);

       // Create a DataGridColumn, set its header text and other properties
       cs = new DataGridTextBoxColumn();
       cs.MappingName = "Engine.EngineName";
       cs.HeaderText = "Engine";
       cs.Width = engineNameColumnWidth;  
       ItemsTableStyle.GridColumnStyles.Add(cs);

       // Create a DataGridColumn, set its header text and other properties
       cs = new DataGridTextBoxColumn();
       cs.MappingName = "Tires.TireBrand";
       cs.HeaderText = "Tires";
       cs.Width = tireBrandColumnWidth;
       ItemsTableStyle.GridColumnStyles.Add(cs);

The only column showing up on my grid is the CarName column.  How would I
set up the binding for the other two columns without having to set up public
accessors in the car class for each of the properties I want to access in the
classes Engine and Tires?
RobinS - 17 Apr 2007 22:03 GMT
.Net 2.0 or .Net 1.1?

Robin S.
-----------------------
> Can someone help me undestand how to do this properly?
> here's the example class I want to bind to:
[quoted text clipped - 59 lines]
> the
> classes Engine and Tires?
koulbassa - 17 Apr 2007 22:40 GMT
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?

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.