473,574 Members | 2,422 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Combobox databinding bug??

I'm struggling with combobox databinding with something I consider a bug...

I'm binding my combobox to an array of structs. The struct exposes two
public properties, ID and Name, to be used as the
value and displaymember properties.

This works fine. My combobox contains the correct data.

However I'm also binding my SelectedValue property to a column in a
datatable. Here's where I'm lost. The combobox doesn't show the correct
value in the combobox, but is eitther blank or shows the first item.

Below is some test code that produces the same problem:

my struct is defines like so:
public struct ComboboxData

{

private long _ID;

private string _name;

public ComboboxData(lo ng id, string name)

{

_ID = id;

_name = name;

}

public long ID

{

get {return _ID;}

set {_ID = value;}

}

public string Name

{

get {return _name;}

set {_name = value;}

}

}

My array is defined like this:
public ComboboxData[] combdata =

{

new ComboboxData(0, "Test 0"),

new ComboboxData(1, "Test 1"),

new ComboboxData(2, "Test 2")

};

In the form constructor, I set the combobox properties:

this.comboBox1. DataSource = combdata;

this.comboBox1. ValueMember = "ID";

this.comboBox1. DisplayMember = "Name";

this.comboBox1. SelectedValue = 2;

That las line would suggest that when starting the form, the combobox would
display the item where valuemember = 2, which would be "Test 2".

But it doesn't work. When I start the form, my combobox shows "Test 0" and
selectedvalue is 0.
Is this a bug or am I doing something fundamentally wrong?? Any help is
appreciated.
Jan 14 '06 #1
5 3177
Try this:

this.comboBox1. SelectedIndex= this.comboBox1. FindString("Tes t 2");

--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"Peter M." wrote:
I'm struggling with combobox databinding with something I consider a bug...

I'm binding my combobox to an array of structs. The struct exposes two
public properties, ID and Name, to be used as the
value and displaymember properties.

This works fine. My combobox contains the correct data.

However I'm also binding my SelectedValue property to a column in a
datatable. Here's where I'm lost. The combobox doesn't show the correct
value in the combobox, but is eitther blank or shows the first item.

Below is some test code that produces the same problem:

my struct is defines like so:
public struct ComboboxData

{

private long _ID;

private string _name;

public ComboboxData(lo ng id, string name)

{

_ID = id;

_name = name;

}

public long ID

{

get {return _ID;}

set {_ID = value;}

}

public string Name

{

get {return _name;}

set {_name = value;}

}

}

My array is defined like this:
public ComboboxData[] combdata =

{

new ComboboxData(0, "Test 0"),

new ComboboxData(1, "Test 1"),

new ComboboxData(2, "Test 2")

};

In the form constructor, I set the combobox properties:

this.comboBox1. DataSource = combdata;

this.comboBox1. ValueMember = "ID";

this.comboBox1. DisplayMember = "Name";

this.comboBox1. SelectedValue = 2;

That las line would suggest that when starting the form, the combobox would
display the item where valuemember = 2, which would be "Test 2".

But it doesn't work. When I start the form, my combobox shows "Test 0" and
selectedvalue is 0.
Is this a bug or am I doing something fundamentally wrong?? Any help is
appreciated.

Jan 14 '06 #2
Peter,

thanks for your quick response. Your suggestion results in expected
behaviour, however this doesn't solve my problem.

I don't understand why SelectedValue doesn't set the combobox to the right
position.

I've used this setup many times before with succes. However this is the
first time I'm binding to an array of structs, which seems to cause the
problem...

I guess I can always switch to a datatable, but still I'd like to know if
this behaviour is indeed a bug.
"Peter Bromberg [C# MVP]" <pb*******@yaho o.nospammin.com > schreef in bericht
news:C4******** *************** ***********@mic rosoft.com...
Try this:

this.comboBox1. SelectedIndex= this.comboBox1. FindString("Tes t 2");

--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"Peter M." wrote:
I'm struggling with combobox databinding with something I consider a
bug...

I'm binding my combobox to an array of structs. The struct exposes two
public properties, ID and Name, to be used as the
value and displaymember properties.

This works fine. My combobox contains the correct data.

However I'm also binding my SelectedValue property to a column in a
datatable. Here's where I'm lost. The combobox doesn't show the correct
value in the combobox, but is eitther blank or shows the first item.

Below is some test code that produces the same problem:

my struct is defines like so:
public struct ComboboxData

{

private long _ID;

private string _name;

public ComboboxData(lo ng id, string name)

{

_ID = id;

_name = name;

}

public long ID

{

get {return _ID;}

set {_ID = value;}

}

public string Name

{

get {return _name;}

set {_name = value;}

}

}

My array is defined like this:
public ComboboxData[] combdata =

{

new ComboboxData(0, "Test 0"),

new ComboboxData(1, "Test 1"),

new ComboboxData(2, "Test 2")

};

In the form constructor, I set the combobox properties:

this.comboBox1. DataSource = combdata;

this.comboBox1. ValueMember = "ID";

this.comboBox1. DisplayMember = "Name";

this.comboBox1. SelectedValue = 2;

That las line would suggest that when starting the form, the combobox
would
display the item where valuemember = 2, which would be "Test 2".

But it doesn't work. When I start the form, my combobox shows "Test 0"
and
selectedvalue is 0.
Is this a bug or am I doing something fundamentally wrong?? Any help is
appreciated.

Jan 14 '06 #3
Peter,
I think if you set the SelectedIndex instead of the SelectedValue, your
original code should work. I don't know why, but I haven't either the time or
inclination to figure it out, so you are "on your own".
Peter

--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"Peter M." wrote:
Peter,

thanks for your quick response. Your suggestion results in expected
behaviour, however this doesn't solve my problem.

I don't understand why SelectedValue doesn't set the combobox to the right
position.

I've used this setup many times before with succes. However this is the
first time I'm binding to an array of structs, which seems to cause the
problem...

I guess I can always switch to a datatable, but still I'd like to know if
this behaviour is indeed a bug.
"Peter Bromberg [C# MVP]" <pb*******@yaho o.nospammin.com > schreef in bericht
news:C4******** *************** ***********@mic rosoft.com...
Try this:

this.comboBox1. SelectedIndex= this.comboBox1. FindString("Tes t 2");

--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"Peter M." wrote:
I'm struggling with combobox databinding with something I consider a
bug...

I'm binding my combobox to an array of structs. The struct exposes two
public properties, ID and Name, to be used as the
value and displaymember properties.

This works fine. My combobox contains the correct data.

However I'm also binding my SelectedValue property to a column in a
datatable. Here's where I'm lost. The combobox doesn't show the correct
value in the combobox, but is eitther blank or shows the first item.

Below is some test code that produces the same problem:

my struct is defines like so:
public struct ComboboxData

{

private long _ID;

private string _name;

public ComboboxData(lo ng id, string name)

{

_ID = id;

_name = name;

}

public long ID

{

get {return _ID;}

set {_ID = value;}

}

public string Name

{

get {return _name;}

set {_name = value;}

}

}

My array is defined like this:
public ComboboxData[] combdata =

{

new ComboboxData(0, "Test 0"),

new ComboboxData(1, "Test 1"),

new ComboboxData(2, "Test 2")

};

In the form constructor, I set the combobox properties:

this.comboBox1. DataSource = combdata;

this.comboBox1. ValueMember = "ID";

this.comboBox1. DisplayMember = "Name";

this.comboBox1. SelectedValue = 2;

That las line would suggest that when starting the form, the combobox
would
display the item where valuemember = 2, which would be "Test 2".

But it doesn't work. When I start the form, my combobox shows "Test 0"
and
selectedvalue is 0.
Is this a bug or am I doing something fundamentally wrong?? Any help is
appreciated.


Jan 14 '06 #4
Peter,

I cannot use selectedindex.

Anyway I'm tired of this issue. I spend too much time on it already. I'm
rewriting my code to use datatable instead.

thanks anyway,

"Peter Bromberg [C# MVP]" <pb*******@yaho o.nospammin.com > schreef in bericht
news:78******** *************** ***********@mic rosoft.com...
Peter,
I think if you set the SelectedIndex instead of the SelectedValue, your
original code should work. I don't know why, but I haven't either the time
or
inclination to figure it out, so you are "on your own".
Peter

--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"Peter M." wrote:
Peter,

thanks for your quick response. Your suggestion results in expected
behaviour, however this doesn't solve my problem.

I don't understand why SelectedValue doesn't set the combobox to the
right
position.

I've used this setup many times before with succes. However this is the
first time I'm binding to an array of structs, which seems to cause the
problem...

I guess I can always switch to a datatable, but still I'd like to know if
this behaviour is indeed a bug.
"Peter Bromberg [C# MVP]" <pb*******@yaho o.nospammin.com > schreef in
bericht
news:C4******** *************** ***********@mic rosoft.com...
> Try this:
>
> this.comboBox1. SelectedIndex= this.comboBox1. FindString("Tes t 2");
>
> --
> Co-founder, Eggheadcafe.com developer portal:
> http://www.eggheadcafe.com
> UnBlog:
> http://petesbloggerama.blogspot.com
>
>
>
>
> "Peter M." wrote:
>
>> I'm struggling with combobox databinding with something I consider a
>> bug...
>>
>> I'm binding my combobox to an array of structs. The struct exposes two
>> public properties, ID and Name, to be used as the
>> value and displaymember properties.
>>
>> This works fine. My combobox contains the correct data.
>>
>> However I'm also binding my SelectedValue property to a column in a
>> datatable. Here's where I'm lost. The combobox doesn't show the
>> correct
>> value in the combobox, but is eitther blank or shows the first item.
>>
>> Below is some test code that produces the same problem:
>>
>> my struct is defines like so:
>> public struct ComboboxData
>>
>> {
>>
>> private long _ID;
>>
>> private string _name;
>>
>> public ComboboxData(lo ng id, string name)
>>
>> {
>>
>> _ID = id;
>>
>> _name = name;
>>
>> }
>>
>> public long ID
>>
>> {
>>
>> get {return _ID;}
>>
>> set {_ID = value;}
>>
>> }
>>
>> public string Name
>>
>> {
>>
>> get {return _name;}
>>
>> set {_name = value;}
>>
>> }
>>
>> }
>>
>> My array is defined like this:
>> public ComboboxData[] combdata =
>>
>> {
>>
>> new ComboboxData(0, "Test 0"),
>>
>> new ComboboxData(1, "Test 1"),
>>
>> new ComboboxData(2, "Test 2")
>>
>> };
>>
>> In the form constructor, I set the combobox properties:
>>
>> this.comboBox1. DataSource = combdata;
>>
>> this.comboBox1. ValueMember = "ID";
>>
>> this.comboBox1. DisplayMember = "Name";
>>
>> this.comboBox1. SelectedValue = 2;
>>
>> That las line would suggest that when starting the form, the combobox
>> would
>> display the item where valuemember = 2, which would be "Test 2".
>>
>> But it doesn't work. When I start the form, my combobox shows "Test 0"
>> and
>> selectedvalue is 0.
>>
>>
>> Is this a bug or am I doing something fundamentally wrong?? Any help
>> is
>> appreciated.
>>
>>
>>


Jan 15 '06 #5
Peter,

just to let you know.

I actually did use your idea of setting the SelectedIndex property, and it
works like a charm.

so thanks for that suggestion.

"Peter Bromberg [C# MVP]" <pb*******@yaho o.nospammin.com > schreef in bericht
news:78******** *************** ***********@mic rosoft.com...
Peter,
I think if you set the SelectedIndex instead of the SelectedValue, your
original code should work. I don't know why, but I haven't either the time
or
inclination to figure it out, so you are "on your own".
Peter

--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"Peter M." wrote:
Peter,

thanks for your quick response. Your suggestion results in expected
behaviour, however this doesn't solve my problem.

I don't understand why SelectedValue doesn't set the combobox to the
right
position.

I've used this setup many times before with succes. However this is the
first time I'm binding to an array of structs, which seems to cause the
problem...

I guess I can always switch to a datatable, but still I'd like to know if
this behaviour is indeed a bug.
"Peter Bromberg [C# MVP]" <pb*******@yaho o.nospammin.com > schreef in
bericht
news:C4******** *************** ***********@mic rosoft.com...
> Try this:
>
> this.comboBox1. SelectedIndex= this.comboBox1. FindString("Tes t 2");
>
> --
> Co-founder, Eggheadcafe.com developer portal:
> http://www.eggheadcafe.com
> UnBlog:
> http://petesbloggerama.blogspot.com
>
>
>
>
> "Peter M." wrote:
>
>> I'm struggling with combobox databinding with something I consider a
>> bug...
>>
>> I'm binding my combobox to an array of structs. The struct exposes two
>> public properties, ID and Name, to be used as the
>> value and displaymember properties.
>>
>> This works fine. My combobox contains the correct data.
>>
>> However I'm also binding my SelectedValue property to a column in a
>> datatable. Here's where I'm lost. The combobox doesn't show the
>> correct
>> value in the combobox, but is eitther blank or shows the first item.
>>
>> Below is some test code that produces the same problem:
>>
>> my struct is defines like so:
>> public struct ComboboxData
>>
>> {
>>
>> private long _ID;
>>
>> private string _name;
>>
>> public ComboboxData(lo ng id, string name)
>>
>> {
>>
>> _ID = id;
>>
>> _name = name;
>>
>> }
>>
>> public long ID
>>
>> {
>>
>> get {return _ID;}
>>
>> set {_ID = value;}
>>
>> }
>>
>> public string Name
>>
>> {
>>
>> get {return _name;}
>>
>> set {_name = value;}
>>
>> }
>>
>> }
>>
>> My array is defined like this:
>> public ComboboxData[] combdata =
>>
>> {
>>
>> new ComboboxData(0, "Test 0"),
>>
>> new ComboboxData(1, "Test 1"),
>>
>> new ComboboxData(2, "Test 2")
>>
>> };
>>
>> In the form constructor, I set the combobox properties:
>>
>> this.comboBox1. DataSource = combdata;
>>
>> this.comboBox1. ValueMember = "ID";
>>
>> this.comboBox1. DisplayMember = "Name";
>>
>> this.comboBox1. SelectedValue = 2;
>>
>> That las line would suggest that when starting the form, the combobox
>> would
>> display the item where valuemember = 2, which would be "Test 2".
>>
>> But it doesn't work. When I start the form, my combobox shows "Test 0"
>> and
>> selectedvalue is 0.
>>
>>
>> Is this a bug or am I doing something fundamentally wrong?? Any help
>> is
>> appreciated.
>>
>>
>>


Jan 17 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1060
by: eye5600 | last post by:
Creating a windows form to edit address records from a SqlServer table, I want to have a ComboBox for the prefix field (Mr./Mrs./Ms./Dr.). Is it possible to populate the list in the ComboBox AND use databinding to load/save the text? Or is it necessary to set up the ComboBox without databinding and load/save the text with explicit code? The...
1
1164
by: Marius Traelnes | last post by:
Hello! I want to fill a datatable with an unique id and show a combobox based on that id, like this: id combobox 1 value1;value3;value4 2 value5;value6;value7
5
9152
by: Eric A. Johnson | last post by:
Hi Everyone, I am at my wit's end here. I have a combobox (combyQueryTitle) that I need to use in order to select a query for my database project. Therefore, I am using the comboQueryTitle_SelectedIndexChanged handler to, when a new selection is made, update the query and refresh the display. The only problem is that the program seems to...
3
7430
by: Dan Slaby | last post by:
I have a webservice that I want to populate a combobox on a windows form. The webservice creates the correct XML output, but when I attempt to bind it to a combobox I get this error: Additional information: Cannot bind to property or column FullName on DataSource. This error occurs with the databindings. Here is the code: --
8
26405
by: | last post by:
I am sure this has been asked and answered, but here goes anyway... VS.Net 2005, VB.Net How can you display more than one field in the displaymember property of a combobox inside the datagridview control? I am at a loss. Thanks, David
3
6703
by: Magnus | last post by:
Im using a set combobox (ComboBox1) to provide a selection of records from a database table. I have a typed dataset (DataSet1) that contains the typed datatable (DataTable1) that the combobox is bound to. The datatable in the dataset is filled using the typed tableadapter (TableAdapter1). DataTable consists of the typical primary key and value...
3
5297
by: Gerrit | last post by:
Hi, I try to learn programming in c# with databinding controls. Now I have a problem with a ComboBox with the advanced properties for databinding, I want to set the DataSourceUpdateMode to OnPropertyChanged, but then the combobox does not display the right displaymember. Below is my sample code (it is working, when you paste it in a new...
1
4223
by: Andrus | last post by:
I need to enter null value from combobox to business object property. My combobox datasource does not contain ValueMember with null value. So I tried to create combobox which stores null to bound object when text is deleted. However bound object contains old value in this case. To reproduce: 1. Run code
7
13523
by: JTC^..^ | last post by:
When i attempt to bind to the "Text" and "Value" property of a combobox on a windows form the value is reset when I leave the combobox. The comboboxes contain the correct Text and Values. I know this as the Value property binds correctly on it own. It is only when I bind the "Text" and "Value" that the issue occurs. The following sample...
0
8079
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8258
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7833
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8118
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6481
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5321
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3770
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2254
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1359
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.