Combobox Datasource & Binding Custom Objects

D

Demetri

I have a structure called SelectionItem and it has two properties - Key&
Value. I also have a combobox with the DisplayMember = Value and ValueMember
= Key. I have the following simple code:

List<SelectionItem> list = new List<SelectionItem>();
list.Add(new SelectionItem(0, "Item1"));
list.Add(new SelectionItem(1, "Item2"));
myCboBox.DataSource = list;

My issue is that during runtime the cbobox doesn't have Item1, Item2 as
possible values. Rather it has the type name of the object (i.e.
MyApp.UI.Forms.SelectionItem) for each item.

What am I doing wrong?
 
M

miher

Hi,

Are You sure that the DisplayMember and the ValueMember properties are set ?

-Zsolt
 
J

Jeff Johnson

I have a structure called SelectionItem and it has two properties - Key&
Value. I also have a combobox with the DisplayMember = Value and
ValueMember
= Key. I have the following simple code:

List<SelectionItem> list = new List<SelectionItem>();
list.Add(new SelectionItem(0, "Item1"));
list.Add(new SelectionItem(1, "Item2"));
myCboBox.DataSource = list;

My issue is that during runtime the cbobox doesn't have Item1, Item2 as
possible values. Rather it has the type name of the object (i.e.
MyApp.UI.Forms.SelectionItem) for each item.

What am I doing wrong?

Show us the actual definition of the SelectionItem structure, plus the code
that is setting the DisplayMember and ValueMember proeprties on the combo
box.

Personally, I just always override ToString() and provide my display text
that way.
 
J

Jack Jackson

I have a structure called SelectionItem and it has two properties - Key&
Value. I also have a combobox with the DisplayMember = Value and ValueMember
= Key. I have the following simple code:

List<SelectionItem> list = new List<SelectionItem>();
list.Add(new SelectionItem(0, "Item1"));
list.Add(new SelectionItem(1, "Item2"));
myCboBox.DataSource = list;

My issue is that during runtime the cbobox doesn't have Item1, Item2 as
possible values. Rather it has the type name of the object (i.e.
MyApp.UI.Forms.SelectionItem) for each item.

What am I doing wrong?

Is SelectionItem a Structure and not a Class? I don't think this
works for Structures, only Classes.
 
D

Demetri

Sorry, my bad; It is a class:

public class SelectionItem
{
public SelectionItem(int key,string value)
{
Key = key;
Value = value;
}

public int Key
{
get;set;
}

public string Value
{
get;set;
}
}
 
C

coloncm

I hope you got this by now, but you seem to have re-invented the wheel. Why
don't you just the KeyValuePair object, instead. Even if you're doing
something fancy with your SelectionItem, you can pass it to the list of
KeyValuePair objects and easily load the combo box.

Otherwise, it could have something to with setting DisplayMember and
ValueMember properties before setting the DataSource property on the combo
box, which might reset those properties.

Good luck,
 

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