Groups | Blog | Home
all groups > dotnet windows forms databinding > october 2007 >

dotnet windows forms databinding : Databinding to Listcontrol shows the name of the class and not the property


Rahul
10/29/2007 2:09:31 PM
I have a list Control to which I am binding an array of custom objects. Say
the custom object is ClassA. ClassA has a property XYZ. The property XYZ is
of type ClassB. ClassB has a member called "Name". Now I want the "Name" to
appear in the display of the List Control. If I set the DisplayMember
Property to "XYZ.Name" then I get the ClassB as the display rather than the
name.

My question is how do I set the DisplayMember property of a ListControl so
that the property which is 2 level down can be shown as the display for the
List Control.

Thanks
Rahul

Marc Gravell
10/30/2007 12:00:00 AM
On many controls/components there is a DataMember that allows you to
specify a path to the final data, i.e. This.That.TheOther - however,
DisplayMember does not usually allow this level of control. The
property-name entered must exist at the final data-type, otherwise the
default behavior (convert item itslef to string) is invoked.

The simplest option is to add a facade method to your wrapper object,
i.e.
public string XYZName {get {return XYZ.Name;}}
(and set DisplayMember to XYZName)
There are other options involving custom PropertyDescriptors, but to
be honest it probably isn't worth it.

Marc

AddThis Social Bookmark Button