1

我有一个带有 datagridviewcomboboxcolumn 的 datagridview,我在该列上启用了编辑。只要没有为该列设置 datapropertyname,它就可以工作。

当设置了 datapropertyname 并且我在组合框中键入该项目时,它应该被建议,但是当按下 ENTER 时,之前选择的项目再次被选中。

当按下回车后未设置数据属性名时,建议的项目被选中。

我启用编辑的代码:

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
4

1 回答 1

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
于 2016-01-11T11:34:33.583 回答