Binding a Windows Forms ComboBox in C#

by Dave on November 29, 2009

in Windows Forms

Most often when reading the selected item of a bound combobox you will need more information than just the selected text or the selected index of the combo. For example, if you have a combobox bound to a user table in your database, you will most probably want to have the full user name displayed in the combobox, but when a user is selected you will want to work with the user code or user id. In this case the combo’s selected index is of no use and neither is the display text. You need a way to be able to retrieve the user code when a user is selected.

Fortunately, this is quite easy to accomplish. All we need to do is bind our combobox to a list of KeyValuePair objects. In this article I am going to show you how to do exactly that.

To start off create a form which looks similar to the below one:

Binding a ComboBox

Now in the Form_Load event handler we must add the following code:

private void MainForm_Load(object sender, EventArgs e)
{
    // Create a List to store our KeyValuePairs
    List<KeyValuePair<string, string>> data = new List<KeyValuePair<string, string>>();

    // Add data to the List
    data.Add(new KeyValuePair<string, string>("p1", "Joe"));
    data.Add(new KeyValuePair<string, string>("p2", "David"));
    data.Add(new KeyValuePair<string, string>("p3", "Keith"));
    data.Add(new KeyValuePair<string, string>("p4", "Andrew"));
    data.Add(new KeyValuePair<string, string>("p5", "Maria"));
    
    // Clear the combobox
    cboData.DataSource = null;
    cboData.Items.Clear();

    // Bind the combobox
    cboData.DataSource = new BindingSource(data, null);
    cboData.DisplayMember = "Value";
    cboData.ValueMember = "Key";
}

In the first line of code we are creating a List object to store our KeyValuePair objects. The KeyValuePair class can be very useful since it allows you to create a pair of objects of any type, one of which is the key and the other is the value. What this means is that you can have an object of type string paired with a key of type integer for example. Or you could have a value of type MyClass with a key of type string. The combinations are limitless.

As you can see in the code, we are creating a KeyValuePair where both the key and the value are strings. We are then adding data to the List object by creating KeyValuePair instances and adding them to our List. Next we are clearing the combobox as a precaution (just in case it was populated with something already), and finally we are binding the combobox to the List.

The last two lines of the above code are critical for your binding to work correctly. We must tell the combo which is the DisplayMember and which is the ValueMember of our KeyValuePair so that it will know which object from the pair to display and which to use as the key.

Once you have created the above code run your project and you will see that the combobox is populated with the above names. The key value however cannot be seen, so let’s create some code to visually see the selected key.

Subscribe to the SelectedIndexChanged event of your combobox and add the following code:

private void cboData_SelectedIndexChanged(object sender, EventArgs e)
{
    // Get the selected item in the combobox
    KeyValuePair<string, string> selectedPair = (KeyValuePair<string, string>)cboData.SelectedItem;
    
    // Show selected information on screen
    lblSelectedKey.Text = selectedPair.Key;
    lblSelectedValue.Text = selectedPair.Value;
}

What we are doing here is retrieving the combo’s selected item and converting it to a KeyValuePair object. Then from our KeyValuePair object we are accessing the Key and the Value properties and assigning them to the labels on our form.

Now if you run the application again you should have something like the following, and as you can see the key and value are being displayed in our labels.

Binding a ComboBox

Now using this technique you can easily bind a combobox and have one value displayed while another value is used as your key.

I hope you found this useful.
Dave.



{ 9 comments… read them below or add one }

roblesrt June 18, 2010 at 01:07

thanks Dave, this saves me a lot of time and is better example compared to a myriad of examples binding combobox in .net

Reply

Dave June 18, 2010 at 09:26

Thanks for your comment. I’m glad you found the article useful.

Reply

Tej Kohli July 19, 2010 at 17:22

Thanks for this Did such thing available in PHP also

Reply

thiri nwe July 30, 2010 at 13:51

Dear sir/madam

Thank a million. Your written codion about combo box is very useful for me .
thans you.

Reply

Dave July 30, 2010 at 20:59

Glad you found it helpful.

Reply

Michael Shelnutt July 18, 2011 at 06:12

This was perfect! – just what I was looking for – clean and simple

Reply

Evandro Junqueira Ramos September 6, 2011 at 10:57

Nice!! I was looking for something like this….
I will try!

Thank you!

Reply

Nikhil July 4, 2012 at 15:55

Hi, Nice article.. very helpful to me ….just what I needed. Thanks!

Reply

rashmi July 20, 2012 at 22:53

Hello sir can u just expalin me how to do bind this from the acces database and once we select from the comboits ID should save in the database
how to do this…………..

Reply

Leave a Comment

Previous post: