简体   繁体   中英

Datagridview DatagridviewComboboxColumn Editing DataBound

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
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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM