从vb.net

时间:2016-07-21 16:49:42

标签: vb.net datagridview

我正在尝试从datagridview中删除当前行,该行未绑定到任何数据源,并且AllowUserToAddRows属性为False。

我使用代码

向datagridview添加行
  Purchases.Rows.Add(selectedrow.Cells(0).Value, selectedrow.Cells(1).Value, selectedrow.Cells(2).Value, selectedrow.Cells(6).Value, "", "", selectedrow.Cells(3).Value

我使用此代码删除当前行

  Private Sub Purchases_CellEndEdit(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles Purchases.CellEndEdit
    Try
        If CDbl(Purchases.CurrentRow.Cells("Column7").Value) - CDbl(Purchases.CurrentRow.Cells(4).Value) < 0 Then
            MessageBox.Show("Quantity entered is more than total in Stock " & vbNewLine & vbNewLine & Purchases.CurrentRow.Cells(1).Value & " Has " & stock & " in Stock" & vbNewLine & vbNewLine & "Increase Stock Level or Reduce Quantity Entered or See System Administrator for Help", "Pharm App " & Today.Year & "", MessageBoxButtons.OK, MessageBoxIcon.Information)
            Purchases.Rows.Remove(Purchases.CurrentRow)
        End If
    Catch ex As Exception
        MsgBox(ex.Message)
    End Try
End Sub

但我收到错误

&#34;操作无效,因为它导致重新调用SetCurrentCellAddressCore函数&#34;

我尝试用

删除最后一行
   Purchases.Rows.RemoveAt(Purchases.RowCount - 1)

我得到同样的错误。

请帮忙,因为我是新手。

2 个答案:

答案 0 :(得分:0)

试试这个,它适用于我

DataGridView1.AllowUserToAddRows = False

答案 1 :(得分:0)

您必须在 CellEndEdit事件后删除行,因此请替换此行:

Purchases.Rows.Remove(Purchases.CurrentRow)

用这个:

Me.BeginInvoke(New Action(Sub() Purchases.Rows.Remove(Purchases.CurrentRow)))