1

I have a datagridview with a datagridviewcomboboxcolumn on which I enabled editing. This works as long as no datapropertyname is set for the column.

when the datapropertyname is set and i'm typing in the combobox the item is suggested as it should, but when pressing ENTER the previously selected item is again selected.

While when the datapropertyname is not set after pressing enter the suggested item is selected.

My code to enable editing:

Private Sub DataGridView1_EditingControlShowing(ByVal sender As Object, ByVal e As DataGridViewEditingControlShowingEventArgs) Handles DataGridView1.EditingControlShowing
    If e.Control.GetType Is GetType(DataGridViewComboBoxEditingControl) Then
        Dim cb As ComboBox = TryCast(e.Control, ComboBox)
        If cb IsNot Nothing Then
            cb.DropDownStyle = ComboBoxStyle.DropDown
        End If
    End If
End Sub
Michiel Alders
  • 69
  • 1
  • 2
  • 15
  • Are you setting your update mode to OnValidation for the DataSource? I always set my ComboBoxStyle to simple to edit or add if it is left on DropDownList you will have index issues at some point. – Bit May 18 '11 at 11:12
  • Thats why its set to DropDown, i still need the Dropdown button so Simple would mean a textfield. It's just that when typing in the combobox the value is suggested from the items in the combobox but not selected when pressed on enter. – Michiel Alders May 18 '11 at 11:20
  • I normally choose the item to edit then set the combobox to simple edit, then save the updated dataset. – Bit May 18 '11 at 12:27
  • I don't exactly understand what you mean by that. Any lines of code to help me on my way? – Michiel Alders May 18 '11 at 13:18

1 Answers1

0
Private Sub grdReceiving_EditingControlShowing(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles grdReceiving.EditingControlShowing
    If e.Control.GetType Is GetType(DataGridViewComboBoxEditingControl) Then
        Dim cb As ComboBox = TryCast(e.Control, ComboBox)
        If cb IsNot Nothing Then
            cb.DropDownStyle = ComboBoxStyle.DropDown
        End If
    End If
End Sub
Private Sub grdReceiving_CellValidating(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellValidatingEventArgs) Handles grdReceiving.CellValidating
    Select Case e.ColumnIndex
        Case 4
            Dim comboBoxColumn As DataGridViewComboBoxColumn = grdReceiving.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
            grdReceiving.Rows(e.RowIndex).Cells(e.ColumnIndex).Value = e.FormattedValue
        Case 5
            Dim comboBoxColumn As DataGridViewComboBoxColumn = grdReceiving.Columns(5)
            If (e.ColumnIndex = comboBoxColumn.DisplayIndex) Then
                If (Not comboBoxColumn.Items.Contains(e.FormattedValue)) Then
                    comboBoxColumn.Items.Add(e.FormattedValue)
                End If
            End If
            grdReceiving.Rows(e.RowIndex).Cells(e.ColumnIndex).Value = e.FormattedValue
        Case 6
            Dim comboBoxColumn As DataGridViewComboBoxColumn = grdReceiving.Columns(6)
            If (e.ColumnIndex = comboBoxColumn.DisplayIndex) Then
                If (Not comboBoxColumn.Items.Contains(e.FormattedValue)) Then
                    comboBoxColumn.Items.Add(e.FormattedValue)
                End If
            End If
            grdReceiving.Rows(e.RowIndex).Cells(e.ColumnIndex).Value = e.FormattedValue
    End Select
End Sub
  • 1
    Welcome to Stackoverflow! Thank you for taking the time to answer this question. You may want to review this link http://stackoverflow.com/help/how-to-answer on answering questions. You should try to expand your answer to include more than just code. – Peter Hornsby Jan 11 '16 at 11:40