471,312 Members | 1,774 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,312 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 3017
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by eye5600 | last post: by
1 post views Thread by Marius Traelnes | last post: by
5 posts views Thread by Eric A. Johnson | last post: by
3 posts views Thread by Dan Slaby | last post: by
8 posts views Thread by | last post: by
3 posts views Thread by Gerrit | last post: by
1 post views Thread by Andrus | last post: by
reply views Thread by rosydwin | last post: by

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.