Edit DataGridView comboBox

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?

Cheers
LVL 3
FMabeyAsked:
Who is Participating?
 
wsh2Connect With a Mentor Commented:
Couretesy of DevX.com
http://www.devx.com/dotnet/Article/33748/0/page/3
-------------------------------------------------------------------------------------------------------
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 = _
           DataGridView1.Columns(4)
        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 = _
           DataGridView1.Columns(4)
        If (e.ColumnIndex = comboBoxColumn.DisplayIndex) Then
            If (Not comboBoxColumn.Items.Contains( _
               e.FormattedValue)) Then
                comboBoxColumn.Items.Add(e.FormattedValue)
            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.
-------------------------------------------------------------------------------------------------------
0
 
FMabeyAuthor Commented:
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.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.