ComboBox.SelectedValue is not Working
This does not answer the OP however...the ComboBox SelectedValue must be an integer type.
If you have a short or byte var that holds the value that will set the SelectedValue, it won't work - you will have null/nothing value.
Use an integer.
Both ValueMember
and DisplayMember
properties are used only if DataSource
property is defined.
So, you should re-write your code as follows:
private readonly BindingList<KeyValuePair<string, int>> m_items =
new BindingList<KeyValuePair<string, int>>();
public YourForm()
{
InitializeComponent();
cboGridSize.DisplayMember = "Key";
cboGridSize.ValueMember = "Value";
cboGridSize.DataSource = m_items;
for (int i = 2; i <= 12; i++)
m_items.Add(new KeyValuePair<string,int>(i.ToString(), i));
cboGridSize.SelectedValue = 4;
}
Links:
- BindingList class
- ObservableCollection class
- INotifyCollectionChanged Interface