Using the DataGridViewComboBoxColumn to display a dropdown list in C# (VS 2005)

In this article we will implement the DataGridViewComboBoxColumn component to display a dropdown list (combo box) as one of the columns in our DataGridView.

This article is a continuation of the previous article
Using a DataGridView to display data from a database in Visual Studio 2005 (C#)

If you are familiar with the DataGridView component, you can skip the previous article. It merely shows you how to initially display data in your DataGridView.

In this article I assume that you are continuing with the previous article's code. You may of course, simply download the complete project and follow the article in there.

Got questions about C# or .Net? Go to our Dot Net Forum.

Downloads
DataGridViewComboboxColumn-Sample.zip (Excludes Database) - 21MB
MS Access database (zipped) - 8KB

Please note
The project in the zipped file under downloads has the same name as the project from the previous example. If you extract it without specifying a different directory it will override your previous project.

The MS Access database needs to be unzipped in the bin\debug directory of your project. See the previous article for the tables involved if you wish to create the database yourself.

In the previous article we duscussed the DataGridView component and used it to display a title (Mr, Mrs, Ms) and person's name from our Microsoft Access database. Here we will extend that project so that the Title column becomes a combobox where our users can select the title from a list instead of having to type it in.

I know it's not good coding practice, but for the sake of simplicity I am going to add the new code for this article to the end of the Form1_Load() method.

Up and running
Add the following code to the end of your Form1_Load() method and when you run your application (in debug mode), the Title column should now be a combobox where you can select the title from a list. At this stage your People table in the database MUST contain valid entries in the PTitle column. Valid means only entries found in the Titles table.

Now lets briefly run through the 4 blocks of code above.

Remove the existing column
The first line of code simply removes the existing ptitle column from dataGridView1. We had to include it in the SQL select and then remove it here. If you simply left it out of the SQL select statement, the new DataGridViewComboBoxColumn would not be linked (bound) to the ptitle column in the People table. If you want to update your database from the DataGridView in the future, the columns must be bound to a column in the database.

Create a DataGridViewComboBoxColumn object
The next block of code creates a DataGridViewComboBoxColumn object for us. I will not explain all the properties here as most of them are self-explanatory. What is important is that we are setting the DataPropertyName to ptitle (same as the column we just removed from dataGridView1).

Get the data from the Combobox
In the third block we populate the DataGridViewComboBoxColumn with values from the Titles table in our database. Note that this is practically the same as populating dataGridView1 in the previous article.

Have a look at the previous article if you do not understand any of these points

Populate the DataGridViewComboBoxColumn
Now that we have all the titles in the DataSet (Cds), we tell the DataGridViewComboBoxColumn that we created in the second block of code that it should use this new DataSet as its source. The ValueMember property is the name of the column in this datasource (Cds) that should be stored in this DataGridViewComboBoxColumn whereas the DisplayMember is the name of the column that should be displayed to the user. In our example the two should be the same.

Add the new DataGridViewComboBoxColumn to your DataGridView
The last step is to insert this new DataGridViewComboBoxColumn into our dataGridView1. The first argument, "1", is the index of column in front of which you want to insert the new column. As most thing in C#, the column index is zero based, but keep in mind that our first column "PersonID" is hidden (we set its Visible property to false in the previous article). So by specifying "1" as the column index, we are actually inserting this new column between the "PersonID" and "PName" columns.

Them bugs
At this stage our application should work and allow the user to select a title from a dropdown list. That is, it should work if you have at least 1 record in your People table and all rows in your People table contains a valid entry (a value that can be found in the Titles table. However, try to click on the Title column for a new row (the last row within dataGridView1 should be a new row). The application crashes with an "index out of range error"!!

This little problem is the reason I decided to write this and the previous article. Last night at about 11:30 I was ready to change this stupid PC into a footstep for my pet elephant and take up beachbumming as a career. Between Google, Yahoo, MSN and AltaVista I found 5 documents when searching for DataGridViewComboBoxColumn. Beloved Microsoft could not find a single entry on their own website (www and MSDN) about their own class (19 October 2004)!! 2 of the 5 documents were in Spanish and "I don't speak Espanol", one was to mention that project Mona (or something) did not include DataGridViewComboBoxColumn. The last 2 documents were posts in a forum by the same guy, one to mention that he had some problem with DataGridViewComboBoxColumn and the next that he had found the solution. His problem was not the same as mine and he did not include his solution anyway. Oh, and furthermore beloved Microsoft conveniently decided to set the AllowUserToAddRows property to false in their Visual Studio documentation so that this problem does not occur!! Sorry, me little bit frustrated, but this is the reason I decided to write these two articles.

Okay, back to reality. After all that, the solution to this problem is very simple. We only need to set a default value for the column. In the original code for Form1_Load() method (before the new code from this article), search for
dbDA.Fill(ds1, "results");
and add the following line after that line

ds1.Tables[0].Columns[1].DefaultValue = "Mr";

Conclusion
That's it. You should now have a DataGridView which displays data from your Access database and displays the Title column as a combobox from which your users can select.
By the way, for something as static as Titles, you could hardcode the values with DataGridViewComboBoxColumn.Items.AddRange. However, the idea here was to show how to create a comboBox from a database table.

About the author
Dirk Wessels is a software developer and owner of www.getquotes-it.com. You may redistribute this article, but please keep this “About the author” section within the article together with an active link to www.getquotes-it.com