1

Similar to a previous question but for .NET 4.5, where the accepted answer doesn't work when I try it.

I'm trying to populate a ComboBox from a Dictionary. No compiler errors, but I get an ArgumentException: "Complex DataBinding accepts as a data source either an IList or an IListSource". That makes me think that the way I'm binding, ComboBox is only going to let me fill either one or the other, since List is only one dimensional.

Simplified code:

Dictionary<string,string> orgs = await api.CreateOrgMap();
organizationListComboBox.DataSource = orgs; 

I could use a List of an object with the fields Key and Value, but that seems silly when Dictionary should work and ends up with a bonus object. What am I doing wrong / is this no longer possible?

I'm not sure it makes a difference, but I'm using WinForms.

Community
  • 1
  • 1
William
  • 188
  • 1
  • 8
  • Do you want both Key and Value to show up in the list, is that what you are trying to accomplish? – SwDevMan81 Dec 19 '14 at 21:11
  • Nope. I want to be able to select a Key from the ComboBox and get the matching Value. Searching through the Dictionary's Keys to get that seems like the wrong direction to go though. – William Dec 19 '14 at 21:15
  • Can you post whats wrong with the solution provided in the question you reference: http://stackoverflow.com/a/6412893/95573 – SwDevMan81 Dec 19 '14 at 21:16
  • Sure! It throws an ArgumentException when `organizationListComboBox.DataSource = orgs;` is called – William Dec 19 '14 at 21:23

2 Answers2

2

I think you might be not following the example in the referenced topic?

organization.ListComboBox.DataSource = orgs; 

Should be:

organization.ListComboBox.DataSource = new BindingSource(orgs, null);
Nikolay
  • 10,752
  • 2
  • 23
  • 51
  • 1
    ... thanks! I tried that constructor, but I must have skipped _exactly what's written in the answer_. – William Dec 19 '14 at 21:28
  • I just tried, but I don't have enough rep to upvote your answer, but thank you again! – William Dec 19 '14 at 22:05
  • Thanks! works great.. this is the winning bid! Do you know how to get rid of the displayed brackets.. [ ..value.. ] – Leo Gurdian Dec 30 '16 at 03:56
0

I use List's all the time in VB.NET. A dictionary should work in the same way.

cmbox.DataSource = GetStores(); //this function is returning a list
cmbox.DisplayMember = "Joined"; //this is a property for an item in the list
cmbox.ValueMember = "ID"; //this is another property for an item in the list

Joined = Key ID = Value - or vise-versa hope the C# translation is correct, and hope it helps.

It also might be easier to loop through your dictionary to make a list IE drop the keys then throw the list into the combo box. since your not using the keys for the combo box.

nward32
  • 1
  • 2
  • In this case, GetStores() is returning an object equivalent to List<{string ID, string Joined}>, right? – William Dec 19 '14 at 21:20
  • Yeah, which is why I would think a dictionary could do the same with Key and Value. (GetStores()) returns many properties but I am only using those two for the combobox). – nward32 Dec 19 '14 at 21:26