Datagridview DatagridviewCombobox列编辑 DataBound

发布于 2024-11-07 19:24:23 字数 693 浏览 5 评论 0原文

我有一个带有 datagridviewcomboboxcolumn 的 datagridview,我在其中启用了编辑。只要没有为列设置 datapropertyname,此操作就有效。

当设置了 datapropertyname 并且我在组合框中输入时,会按应有的方式建议该项目,但是当按 ENTER 时,会再次选择先前选择的项目。

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

扭转时空 2024-11-14 19:24:23
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
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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文