Combobox databinding bug??

P

Peter M.

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.
 
P

Peter M.

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 said:
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. said:
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.
 
G

Guest

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. said:
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 said:
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. said:
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.
 
P

Peter M.

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 said:
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. said:
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]" <[email protected]> schreef in
bericht
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




:

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.
 
P

Peter M.

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 said:
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. said:
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]" <[email protected]> schreef in
bericht
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




:

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top