473,394 Members | 1,699 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Can't bind a struct to a comboBox

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
5 7617
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: learning_C++ | last post by:
Hi, I found some example code about server and client applications. But I can not run server and client in different computers. Now, I only run them in the different windows in Linux. I hope to...
3
by: MarkDu | last post by:
I want to have a datagrid, say 10 rows, and in one of the columns I want 7 of the rows to have plain text, and the other 3 rows to have a combobox (or linkbutton, etc). Is this even possible? I...
7
by: Dan | last post by:
Very newbie question here: Any suggestions on how to bind an XML file to a combobox and datagrid? I've had no problem reading the data in a datagrid. But the combobox is another story. I...
0
by: Webbyz | last post by:
Here is my problem. I have 2 fields fSeason and rSeason in my database. These hold the String values of either Spring or Fall. I have a comboBox in my form that i am trying to bind on load to...
6
by: s_4 | last post by:
Hello! Sorry about my english - I write to you from poland. I have strange problem. Bind() return '16'. Documentation say that it should return 0 (no error) or -1 (some error). Here is the code...
19
by: active | last post by:
I'm using a ComboBox to display objects of a class I've defined, say CQQ. Works great except somehow I occasionally set an Item to a String object instead of an object of type CQQ. It looks...
11
by: guy | last post by:
I am able to use the Server Explorer to create a dataset containing tables in my database. I am able to bind a table to my combobox and set a filter by including a where clause. But I am unable to...
0
by: Barry Ding | last post by:
#include <map> #include <utility> #include <algorithm> #include <iostream> #include <list> #include <boost/lambda/lambda.hpp> #include <boost/lambda/bind.hpp> using namespace std;
5
by: Mahendra Kumar Kutare | last post by:
I am trying to implement a webserver with boss-worker model thread pool implementation - I have a header declaration threadpool.h as - typedef struct threadpool_work { void (*routine) ();...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.