Can't bind a struct to a comboBox

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;
}
 
M

Morten Wennevik

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";
 
D

DS

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
 
M

Morten Wennevik

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
 
D

DS

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!
 
M

Morten Wennevik

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.
 

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