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

Combobox databinding bug??

P: n/a
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
Share this Question
Share on Google+
5 Replies


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

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

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

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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.