【问题标题】:set new DataGridViewComboBoxCell for after validation of previous DataGridViewTextBoxCell in same column and row为在同一列和同一行中验证先前的 DataGridViewTextBoxCell 后设置新的 DataGridViewComboBoxCell
【发布时间】:2017-02-09 12:56:42
【问题描述】:

我有这个DataGridView,它有一个DataGridViewTextBoxColumn,用户可以在其中输入一个数字,在他输入后我执行搜索以查找该数字下的先前记录。

我想显示这些记录,以便用户可以选择其中一个,或者保留键入的值,这意味着他想创建一条新记录。

为此,我想在用户完成输入后将每个DataGridViewTextBoxCell 替换为DataGridViewComboBoxCell

但是,当我尝试执行此替换时,它会引发此异常:System.Windows.Forms.dll 中的“System.InvalidOperationException”。附加信息:该操作无效,因为它导致重新进入对函数 SetCurrentCellAddressCore 的调用。

这是我的代码(我已经尝试处理 CellLeave 而不是 CellValidated):

Private Sub DataGridViewDebitos_CellValidated(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridViewDebitos.CellValidated
    DataGridViewDebitos.EndEdit(DataGridViewDataErrorContexts.Commit)
    If e.ColumnIndex = ColumnDebito.Index Then
        Dim cellDebito = DataGridViewDebitos.Rows(e.RowIndex).Cells(ColumnDebito.Index)
        Dim numDebito = String.Concat(If(cellDebito.Value, "").ToString.Where(Function(c) Char.IsLetterOrDigit(c)))
        If TypeOf cellDebito Is DataGridViewTextBoxCell AndAlso numDebito.Length >= 3 Then
            Dim prcsa As New List(Of JObject) 'In real version, prcsa is populated by an external function, but it doesn't affect the result
            Dim j = New JObject
            j.SetProperty("id", 0)
            j.SetProperty("nome", cellDebito.Value)
            prcsa.Insert(0, j) 'This option is always present, it allows user to keep simply what was typed
            'Exception hapens here
            DataGridViewDebitos(cellDebito.ColumnIndex, cellDebito.RowIndex) =
                New DataGridViewComboBoxCell With {
                    .DataSource = prcsa,
                    .DisplayMember = "nome",
                    .FlatStyle = FlatStyle.Flat,
                    .ValueMember = "id",
                    .Value = prcsa(0).Value(Of Integer)("id")}
        End If
    End If
End Sub

非常感谢

【问题讨论】:

    标签: vb.net winforms datagridview datagridviewcomboboxcell datagridviewtextboxcell


    【解决方案1】:

    我已经通过 .BeginInvoke 将有问题的语句放入 Lambda 子程序中,这解决了问题,只是我不知道为什么......

    谁能给我解释一下它的机制?谢谢!

    Private Sub DataGridViewDebitos_CellValidated(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridViewDebitos.CellValidated
        DataGridViewDebitos.EndEdit(DataGridViewDataErrorContexts.Commit)
        If e.ColumnIndex = ColumnDebito.Index Then
            Dim cellDebito = DataGridViewDebitos.Rows(e.RowIndex).Cells(ColumnDebito.Index)
            Dim numDebito = String.Concat(If(cellDebito.Value, "").ToString.Where(Function(c) Char.IsLetterOrDigit(c)))
            If TypeOf cellDebito Is DataGridViewTextBoxCell AndAlso numDebito.Length >= 3 Then
                Dim prcsa As New List(Of JObject) 'In real version, prcsa is populated by an external function, but it doesn't affect the result
                Dim j = New JObject
                j.SetProperty("id", 0)
                j.SetProperty("nome", cellDebito.Value)
                prcsa.Insert(0, j) 'This option is always present, it allows user to keep simply what was typed
                'Exception hapens here
                DataGridViewDebitos.BeginInvoke(
                    Sub()
                        DataGridViewDebitos(cellDebito.ColumnIndex, cellDebito.RowIndex) =
                            New DataGridViewComboBoxCell With {
                            .DataSource = prcsa,
                            .DisplayMember = "nome",
                            .FlatStyle = FlatStyle.Flat,
                            .ValueMember = "id",
                            .Value = prcsa(0)("id")}
                    End Sub)
            End If
        End If
    End Sub
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-12-28
      • 1970-01-01
      • 2018-12-11
      • 2019-08-09
      • 2017-08-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多