ComboBox in DataGridView

One of my favorite tools in Visual Studio is the DataGridView control. While making an admin page for a project that I’m working on, I figured out how to use a combo box instead of the default text field. The whole process is pretty straightforward in both VS 2005 and 2008:

Step 1. After creating your DataGridView and binding it to your data source, click on the small arrow and select ‘Edit Columns…’

Step 2. Click ‘Add…’ to insert a new column into the DataGridView control. Select the column that you would like to use a combo box for, change the type to ‘DataGridViewComboBoxColumn’ and click Add when finished.

Step 3. Alright – you’ve got a combo box in your DataGridView control. Now how do we populate it with data? In your code-behind page, insert the following code in your Form_Load event:

Dim dgvc As DataGridViewComboBoxColumn
dgvc = DataGridView1.Columns("columnName")
dgvc.Items.add("Amazing Item")
dgvc.Items.add("Sweet Item")

There you have it!

Leave a Comment

Please note: Comment moderation is enabled and may delay your comment. There is no need to resubmit your comment.