Can't bind a struct to a comboBox

  • Thread starter Thread starter DS
  • Start date Start date
D

DS

How do I go about binding a "struct" datatype to a comboBox? When I bind it
by means of the DataSource, it doesn't give any error, but the comboBox just
gets populated with "WindowsApplication2.Form1+Unit". Uncommenting the
DisplayMember line has no effect. Uncommenting the ValueMember results in a
crash: "Could not bind to the new display member\nParameter name:
newDisplaymember". How can I get this to work? Thanks in advance.

Here is the code:

private void button3_Click(object sender, System.EventArgs e)
{
Unit[] unit = new Unit[3];
unit[0].id = 0;
unit[0].text = "honda";
unit[1].id = 1;
unit[1].text = "gm";
unit[2].id = 2;
unit[2].text = "toyota";

MessageBox.Show(unit[0].text); // correctly shows honda

comboBox1.DataSource = unit;
//comboBox1.ValueMember = "id";
//comboBox1.DisplayMember = "text";
}

private struct Unit
{
public string text;
public int id;
}
 
Hi DS,

You need properties for the display and value. Change your struct to

struct Unit
{
public string text;
public int id;
public string Text
{
get{return text;}
}
public int Id
{
get{return id;}
}
}

...

comboBox1.ValueMember = "Id";
comboBox1.DisplayMember = "Text";
 
Morten Wennevik said:
Hi DS,

You need properties for the display and value. Change your struct to

struct Unit
{
public string text;
public int id;
public string Text
{
get{return text;}
}
public int Id
{
get{return id;}
}
}

..

comboBox1.ValueMember = "Id";
comboBox1.DisplayMember = "Text";

Brilliant! Thank you very much Morten! I spent 3 hours trying various things
to get it to work with no luck, and now you got it working :). I checked the
docs and no where do they say that you have to setup properties to access
the fields in the Struct. In fact, doesn't that sort of go against the
reason that its "acceptable" to make the fields public and access them
directly? Am I missing something here?

As a followup, I seem to have found another mistery. It displays "toyota" in
the comboBox, and I can grab the correct SelectedValue (which it gives me
correctly from the ValueMember property), but why is it that when I try to
grab the SeletecdItem property I once again get
"WindowsApplication2.Form1+Unit"?

Thanks again,
Dan
 
Brilliant! Thank you very much Morten! I spent 3 hours trying various
things
to get it to work with no luck, and now you got it working :). I checked
the
docs and no where do they say that you have to setup properties to access
the fields in the Struct. In fact, doesn't that sort of go against the
reason that its "acceptable" to make the fields public and access them
directly? Am I missing something here?
Can't say
As a followup, I seem to have found another mistery. It displays
"toyota" in
the comboBox, and I can grab the correct SelectedValue (which it gives me
correctly from the ValueMember property), but why is it that when I try
to
grab the SeletecdItem property I once again get
"WindowsApplication2.Form1+Unit"?

Thanks again,
Dan

The ComboBox stores objects. The displaymember and valuemember specifies
what parts of that object should be used in the ComboBox but SelectedItem
returns the whole object, in your case a Unit. I suspect you call
ToString() on that object, so in effect all you get is the object name
which is NameSpace.ClassName+StructName.

You need to cast it back to a Unit and then grab its Text (or text).

((Unit)(ComboBox1.SelectedItem)).Text
 
Morten Wennevik said:
Can't say


The ComboBox stores objects. The displaymember and valuemember specifies
what parts of that object should be used in the ComboBox but SelectedItem
returns the whole object, in your case a Unit. I suspect you call
ToString() on that object, so in effect all you get is the object name
which is NameSpace.ClassName+StructName.

You need to cast it back to a Unit and then grab its Text (or text).

((Unit)(ComboBox1.SelectedItem)).Text

Sweet! You did it again. Thanks! :-) The help and speed is much appreciated!
Kudos :)

So just to clarify, should I be making my struct fields private and then
having accessor properties? Thanks again man.

Cheers!
 
Sweet! You did it again. Thanks! :-) The help and speed is much
appreciated!
Kudos :)

So just to clarify, should I be making my struct fields private and then
having accessor properties? Thanks again man.

Cheers!

Well, I'm not the one to advice on accessibility, but I would probably
have made the fields private.
 
Back
Top