Combobox Datasource & Binding Custom Objects

  • Thread starter Thread starter Demetri
  • Start date Start date
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?
 
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.
 
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.
 
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;
}
}
 
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,
 
Back
Top