Click here to Skip to main content
15,896,456 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi friends...

Whats the best way to bind a Dictionary to COmbobox?
I have used this...

C#
Dictionary<string,string> ldict = new Dictionary<string,string>();
ldict.Add("English", "En.dll");
ldict.Add("German", "Ge.dll");
ldict.Add("French", "Fr.dll");
comboBox2.DataSource = ldict.ToList();
               comboBox2.ValueMember = "Value";
               comboBox2.DisplayMember = "Key";


combobox2.SelectedValue gives me the correct selected value.... But I cannot get the Selected Key...
If I call combobox2.SelectedItem It returns the whole Dictionary object....
Please suggest me what should I do...

Thanks
Posted
Comments
Pheonyx 13-Aug-13 4:16am    
What exactly is your issue? Why do you need the key if you have the selected value?
Yesudasan Moses 13-Aug-13 4:19am    
Two things I am doing when people change the language...
1. Change the Language of UI = Path ie, value is needed
2. setting the selected Language as Default in Database,, = Key is needed for comparing the row in db...

1 solution

I would do the following:

C#
Dictionary<string,string> ldict = new Dictionary<string,string>();
ldict.Add("English", "En.dll");
ldict.Add("German", "Ge.dll");
ldict.Add("French", "Fr.dll");
comboBox2.DataSource = ldict.ToList();
               comboBox2.ValueMember = "Key";
               comboBox2.DisplayMember = "Key";


Then in the select change you can get the value from the dictionary using the key that has been selected. There is probably a better way but I'm not sure what it would be.
 
Share this answer
 
Comments
Yesudasan Moses 13-Aug-13 7:27am    
Thanks bro... It works...
Pheonyx 13-Aug-13 7:36am    
No problem.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900