1

I am trying to initialize a ComboBox from a Dictionary as follows:

Dictionary<string, int> TestDictionary = new Dictionary<string, int>
{
    {"Text1", 1},
    {"Text2", 2},
    {"Text3", 3}
};

testComboBox.DataSource = new BindingSource(TestDictionary, null);

But this throws the following exception:

ArgumentNullException

James Allman
  • 40,573
  • 11
  • 57
  • 70
  • Possible duplicate, See here [binding-combobox-using-dictionary-as-the-datasource][1] [1]: http://stackoverflow.com/questions/6412739/binding-combobox-using-dictionary-as-the-datasource – nobody Mar 17 '15 at 21:07

1 Answers1

0

Why are you binding it to a null? https://msdn.microsoft.com/en-us/library/system.windows.forms.bindingsource%28v=vs.110%29.aspx You want this guy

BindingSource(IContainer)

not this one

BindingSource(Object, String)

Since

public BindingSource(
    Object dataSource,
    string dataMember
)

dataMember is used by your combobox.

dataMember
Type: System.String
The specific column or list name within the data source to bind to.

Or give it a proper name.

UPD/Correction:

According to the comment up top, you need to map these two:

testComboBox.DisplayMember = "Value";
testComboBox.ValueMember = "Key";
Serj
  • 26
  • 4