Edit DataGridView comboBox

Posted on 2007-08-10
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

    Couretesy of
    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.
    LVL 3

    Author Comment

    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 Trending Threat Insights Every Day

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Join & Write a Comment

    This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
    I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

    755 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

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now