0

以前DataGridViewTextBoxCell的验证后,我有这个DataGridView,它有一个DataGridViewTextBoxColumn这里用户可以输入一个数字,他的类型后,我进行搜索,找到下面这个数字以前的记录。设置新的DataGridViewComboBoxCell在同一列和

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

为此,我想用替换每个DataGridViewTextBoxCell这些选项,当用户输入完成时。

但是,当我尝试执行此替换时引发此异常: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 

非常感谢您

回答

0

我已经通过.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 
相关问题