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