Home > DeveloperSection > Forums > How to get Combobox.Datasource to a Dictionary?
Takeshi Okada
Takeshi Okada

Total Post:89

Posted on    January-27-2014 3:03 AM

 C# C# 

 1 Reply(s)
 1697  View(s)
Rate this:

Following is my code which is binding a List() to a ComboBox control. I tried to add few items to the CombBox control using Insert() method which is not allowed as it has been assigned to a datasource. So, how can I get the datasouce back to a new variable (say, var colours2) from cmbColour.DataSource which is returning an object. Thanx !

var colours= new Dictionary<string, string>                                                                                                







cmbColour.ValueMember = "Key";

cmbColour.DisplayMember = "Value";

cmbColour.DataSource = colours;

var colours2 = //how can I get the DataSource back

Pravesh Singh

Total Post:412

Posted on    January-27-2014 3:20 AM

Hi Takeshi,

Try this:-

var coloursList = cmbColour.DataSource as List<KeyValuePair<string, string>>;

var colours2 = coloursList.ToDictionary(x => x.Key, x => x.Value);

Don't want to miss updates? Please click the below button!

Follow MindStick