[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now


Edit DataGridView comboBox

Posted on 2007-08-10
Medium Priority
Last Modified: 2008-01-09
Hi all,

I am using VS2005 (VB.NET) winforms.
I have a datagridview which has one column using the DataGridViewComboBoxColumn column type. This is bound to a table in SQL Server through a dataset. If the user doesn't find the value they are  looking for in the dopdown, I would like them to be able to type in their own value. At the moment this doesn't allow them to do this.

Is there a setting I need to switch on/off to enable this, or is there a piece of code I can use?

Question by:FMabey
LVL 14

Accepted Solution

wsh2 earned 1000 total points
ID: 19668503
Couretesy of DevX.com
Adding Items into a DataGridViewComboBoxColumn Control
There are times when you need to allow the users to insert additional items into the ComboBox control. In this case, you need to do some work.

First, service the EditingControlShowing event of the DataGridView control. This event is fired when the user tries to edit the ComboBox control:

    Private Sub DataGridView1_EditingControlShowing( _
       ByVal sender As Object, _
       ByVal e As System.Windows.Forms. _
          DataGridViewEditingControlShowingEventArgs) _
       Handles DataGridView1.EditingControlShowing

        Dim comboBoxColumn As DataGridViewComboBoxColumn = _
        If (DataGridView1.CurrentCellAddress.X = _
           comboBoxColumn.DisplayIndex) Then
            Dim cb As ComboBox = e.Control
            If (cb IsNot Nothing) Then
                cb.DropDownStyle = ComboBoxStyle.DropDown
            End If
        End If
    End Sub

Here, you essentially check to see if the cell being edited is the one containing the ComboBox. If it is, the drop-down style of the ComboBox control is set to DropDown so that the user can type into it.
Next, service the CellValidating event of the DataGridView control. This event is fired when the user is done with the typing and leaves the cell:

    Private Sub DataGridView1_CellValidating( _
       ByVal sender As Object, _
       ByVal e As System.Windows.Forms. _
       DataGridViewCellValidatingEventArgs) _
       Handles DataGridView1.CellValidating

        Dim comboBoxColumn As DataGridViewComboBoxColumn = _
        If (e.ColumnIndex = comboBoxColumn.DisplayIndex) Then
            If (Not comboBoxColumn.Items.Contains( _
               e.FormattedValue)) Then
            End If
        End If
    End Sub

Here, you will perform the usual checks and then add the newly typed item into the ComboBox. Figure 12 shows that originally the ComboBox contains three items. The user can now type in a new item (e.g. "Type D") and then it will be added into the list. Note that for subsequent rows (as well as the rows above it) the ComboBox control will now contain four items.

Author Comment

ID: 19690936
I am still looking at this but a few other things have come up... I'll get back when I've got something further to comment.

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question