Databinding problem - DisplayName empty

M

mcollier

I'm having some trouble getting databinding to a ListBox to work
correctly. I think the collection I'm trying to bind against is a
little odd, so I'm wondering if that could be part of the problem.

Below is a sample of the databinding code.
AxisCollection axis = GetAxisCollection()
myListBox.DataSource = null;
myListBox.Items.Clear();
myListBox.DisplayMember = "Attribute.Name";
myListBox.ValueMember = "Attribute.AttributeID";
myListBox.DataSource = attribs;

AxisCollection is a collection which derives from List<Axis>.

The Axis class looks something like:
Attributes
Name
AttributeID
DisplayNumber

Attributes is a reference type property for the Axis class.

After setting the DataSource property, I notice (using the debugger)
that the DisplayMember property changes to an empty string.

When I look at the displayed values in the listbox, they are just the
name of the class (repeated for each element in the collection).

Interestingly, if I change the myListBox.DisplayName to a value type
(i.e. "DisplayNumber") on the class, the value is displayed correctly
in the ListBox and the DisplayName remains after setting the
DataSource.

Any ideas on what could be going on would be greatly appreciated.


Thanks!
 
M

Morten Wennevik [C# MVP]

Hi,

The DisplayMember turns to an empty string when the ListBox cannot find the
property. I'm not sure what the 'attribs'-reference in your code sample is,
but apparantly it does not contains an "Attribute" property with a "Name"
subproperty. Are you missing an s in Attributes?

myListBox.DisplayMember = "Attributes.Name";
myListBox.ValueMember = "Attributes.AttributeID";
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top