【问题标题】:Datagridview DatagridviewComboboxColumn Editing DataBoundDatagridview DatagridviewComboboxColumn 编辑 DataBound
【发布时间】:2011-05-18 09:55:14
【问题描述】:

我有一个 datagridview 和一个 datagridviewcomboboxcolumn 我启用了编辑。只要没有为该列设置 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

【问题讨论】:

  • 您是否将数据源的更新模式设置为 OnValidation?我总是将我的 ComboBoxStyle 设置为易于编辑或添加,如果它留在 DropDownList 上,您将在某些时候遇到索引问题。
  • 这就是为什么它设置为下拉,我仍然需要下拉按钮,所以简单意味着一个文本字段。只是在组合框中键入时,该值是从组合框中的项目中建议的,但在按下时未选中。
  • 我通常选择要编辑的项目,然后将组合框设置为简单编辑,然后保存更新的数据集。
  • 我不太明白你的意思。有什么代码行可以帮助我吗?

标签: vb.net datagridview datagridviewcombobox


【解决方案1】:
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

【讨论】:

  • 欢迎来到 Stackoverflow!感谢您花时间回答这个问题。您可能想在回答问题时查看此链接stackoverflow.com/help/how-to-answer。您应该尝试扩展您的答案,使其不仅仅包含代码。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-09-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多