By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,282 Members | 1,548 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,282 IT Pros & Developers. It's quick & easy.

Can't bind a struct to a comboBox

P: n/a
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;
}
Nov 16 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
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";

--
Happy Coding!
Morten Wennevik [C# MVP]
Nov 16 '05 #2

P: n/a
DS

"Morten Wennevik" <Mo************@hotmail.com> wrote in message
news:opsfsuf2v8klbvpo@pbn_computer...
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";

--
Happy Coding!
Morten Wennevik [C# MVP]


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
Nov 16 '05 #3

P: n/a
On Wed, 13 Oct 2004 19:58:32 -0400, DS <no****************@rogers.com>
wrote:
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
--
Happy Coding!
Morten Wennevik [C# MVP]
Nov 16 '05 #4

P: n/a
DS

"Morten Wennevik" <Mo************@hotmail.com> wrote in message
news:opsfup7edcklbvpo@pbn_computer...
On Wed, 13 Oct 2004 19:58:32 -0400, DS <no****************@rogers.com>
wrote:
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
--
Happy Coding!
Morten Wennevik [C# MVP]


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!
Nov 16 '05 #5

P: n/a
On Fri, 15 Oct 2004 00:40:37 -0400, DS <no****************@rogers.com>
wrote:

"Morten Wennevik" <Mo************@hotmail.com> wrote in message
news:opsfup7edcklbvpo@pbn_computer...
On Wed, 13 Oct 2004 19:58:32 -0400, DS <no****************@rogers.com>
wrote:
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
--
Happy Coding!
Morten Wennevik [C# MVP]


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.

--
Happy Coding!
Morten Wennevik [C# MVP]
Nov 16 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.