473,416 Members | 1,785 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,416 software developers and data experts.

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(long 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 3163
Try this:

this.comboBox1.SelectedIndex= this.comboBox1.FindString("Test 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(long 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*******@yahoo.nospammin.com> schreef in bericht
news:C4**********************************@microsof t.com...
Try this:

this.comboBox1.SelectedIndex= this.comboBox1.FindString("Test 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(long 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*******@yahoo.nospammin.com> schreef in bericht
news:C4**********************************@microsof t.com...
Try this:

this.comboBox1.SelectedIndex= this.comboBox1.FindString("Test 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(long 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*******@yahoo.nospammin.com> schreef in bericht
news:78**********************************@microsof t.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*******@yahoo.nospammin.com> schreef in
bericht
news:C4**********************************@microsof t.com...
> Try this:
>
> this.comboBox1.SelectedIndex= this.comboBox1.FindString("Test 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(long 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*******@yahoo.nospammin.com> schreef in bericht
news:78**********************************@microsof t.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*******@yahoo.nospammin.com> schreef in
bericht
news:C4**********************************@microsof t.com...
> Try this:
>
> this.comboBox1.SelectedIndex= this.comboBox1.FindString("Test 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(long 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
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...
1
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
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...
3
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...
8
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...
3
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...
3
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...
1
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...
7
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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,...
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
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...
0
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
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...

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.