misterstick
Programmer
trying to set the value of a ComboBox programmatically.
the list contains custom classes with the DisplayMember and ValueMember properties set correctly.
unfortunately, SelectedValue is always null.
is there something i'm missing?
mr s. <
the list contains custom classes with the DisplayMember and ValueMember properties set correctly.
unfortunately, SelectedValue is always null.
is there something i'm missing?
Code:
public class ValuePair
{
// these are properties really
public string KeyValue;
public string DescriptionValue;
ValuePair(string k, string d)
{
KeyValue = k;
DescriptionValue = d;
}
}
class MyForm // edited for brevity
{
private void MyForm_Load(object sender, EventArgs e)
{
comboBox1.ValueMember = "KeyValue";
comboBox1.DisplayMember = "DescriptionValue";
comboBox1.Items.Add(new ValuePair("A", "Value A");
comboBox1.Items.Add(new ValuePair("B", "Value B");
comboBox1.Items.Add(new ValuePair("C", "Value C");
comboBox1.Items.Add(new ValuePair("D", "Value D");
comboBox1.Items.Add(new ValuePair("E", "Value E");
}
private void button1_Click(object sender, EventArgs e)
{
// comboBox1.SelectedValue == null
comboBox1.SelectedValue = "B";
// comboBox1.SelectedValue == null // still!
}
}
mr s. <