A combobox can use
either an Items collection or a DataSource. They are mutually exclusive and the Items collection cannot be modified when a DataSource is used.
As Richard has said you must add and remove items from the datasource.
In choosing a Dictionary as the original datasource you have made this task quite complicated. A dictionary will not bind to a combobox directly as it does not implement the required IList interface. The BindingSource actually creates an intermediate System.ComponentModel.BindingList (populated with KeyValuePairs from dictionary) and then assigns that to the ComboBox.DataSource. In effect the dictionary is not bound to the combobox at all.
Lets assume there is a button which removes the selected item from the combobox. This is the code for it's click handler
private void RemoveFromDictionary_Click(Object sender, EventArgs e) {
if (cbx.SelectedIndex != -1) {
KeyValuePair<int, string> item = (KeyValuePair<int, string>)cbx.SelectedItem;
dic.Remove(item.Key);
cbxBindingSource.DataSource = null;
cbxBindingSource.DataSource = dic;
}
}
If you can redesign your program to use a BindingList directly instead of the dictionary, the code will become much simpler.
private void RemoveFromList_Click(Object sender, EventArgs e) {
if (cbx.SelectedIndex != -1) {
bindlist.RemoveAt(cbx.SelectedIndex);
}
}
Alan.