How To Bind Dictionary To ComboBox In WinForms Using C#

How To Bind Dictionary To ComboBox In WinForms Using C#

Added by Sixth Sense updated on Thursday, December 7, 2017

Problem:

The following code snippet demonstrates how to bind dictionary to combobox in winforms using c#.

Dictionary items = new Dictionary();
items.Add("KEY1", "Value 1");
items.Add("KEY2", "Value 2");
.....
.....
comboBox1.DataSource = new BindingSource(items,null);
comboBox1.DisplayMember = "VALUE";
comboBox1.ValueMember = "KEY";				
				

Or simply

comboBox1.DataSource = items.ToArray()
comboBox1.DisplayMember = "VALUE";
comboBox1.ValueMember = "KEY";								
				

Usage:

Now data is bound to combobox, we can access it by SelectedValue property.

ComboBox1.SelectedValue.ToString()					
					

About

29 Tutorials
25 Snippets
6 Products

More

Contact Us

Contact us

Stay Connected