简体   繁体   English

Datagridview DatagridviewComboboxColumn编辑DataBound

[英]Datagridview DatagridviewComboboxColumn Editing DataBound

I have a datagridview with a datagridviewcomboboxcolumn on which I enabled editing. 我有一个带有datagridviewcomboboxcolumn的datagridview,在其上启用了编辑。 This works as long as no datapropertyname is set for the column. 只要没有为列设置datapropertyname,此方法就起作用。

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. 当设置了datapropertyname且我在组合框中键入内容时,系统会建议该项目,但按ENTER键时,将再次选择先前选择的项目。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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