I'm looking to set the datasource for a combobox. At the same time, i also want to set its display member and value member. Now, i have created a dictionary collection with key(long:value member) and value(string: display member).
Setting this dictionary to combo's data source throws the exception "Complex DataBinding accepts as a data source either an IList or an IListSource". Here is the code(see the data source assignment in the method):
private IDictionary<long, string> Assessments = new Dictionary<long, string>();
public void InitializeSelectedTemplates()
{
if (refreshParameters.SelectedPatient == null)
{
return;
}
vCheckListBoxToolStripItem1.CheckedComboBox.DataSource = Assessments;
vCheckListBoxToolStripItem1.DisplayMember = "Value";
vCheckListBoxToolStripItem1.CheckedComboBox.ValueMember = "Key";
vCheckListBoxToolStripItem1.CheckedComboBox.Items.Clear();
vCheckListBoxToolStripItem1.CheckedComboBox.Items.Add(ResAppConst.GetText("SelectAll"),
false);
}
Note: If i make the following change to datasource assignment i get the exception 'Items collection cannot be modified when the DataSource property is set.'
vCheckListBoxToolStripItem1.CheckedComboBox.DataSource = new BindingSource(Assessments,null);
based on comments i changed the code as below and get the same error as originally reported:
vCheckListBoxToolStripItem1.CheckedComboBox.Items.Add(ResAppConst.GetText("SelectAll"), false);
vCheckListBoxToolStripItem1.CheckedComboBox.DataSource = new BindingSource(Assessments,null);
vCheckListBoxToolStripItem1.DisplayMember = "Value";
vCheckListBoxToolStripItem1.CheckedComboBox.ValueMember = "Key";