简体   繁体   English

从DataGridView删除行

[英]Remove row from a DataGridView

I have a DataGridView with 7 cells, in every cell I'm into if i press escape the current row is removed thou an event i made named Public Event EscapeOnFirstRowPressed with no problem, and brings me the control to each initial state. 我有一个带有7个单元格的DataGridView,如果我按Public Event EscapeOnFirstRowPressed键,我会进入的每个单元格都将删除当前行,这是我创建的名为Public Event EscapeOnFirstRowPressed ,没有任何问题,并将控件带到了每个初始状态。

The general idea of this process is when all the cells are fulfilled then 这个过程的总体思路是当所有单元都满足时

  • Write the Data into a SQL table and 将数据写入SQL表,然后
  • Remove the current row, so the user is now ready to import new data. 删除当前行,以便用户现在准备导入新数据。

Since the Write procedure is OK I turn my interest to remove of the row. 由于“写”过程正常,因此我将兴趣移出该行。

I say this action needs to call it either from RowValidated sub or from CellEnter (which take place when I leave the current row and gets to the next row). 我说这个动作需要从RowValidated子程序或从CellEnter (当我离开当前行并到达下一行时发生)调用它。 But in either place if I put the command 但是在任何地方,如果我输入命令

If Not DGV.CurrentCell.EditedFormattedValue = "" Then
            If Not DGV.Rows(RowIndex).IsNewRow Then
                DGV.Rows.RemoveAt(RowIndex)
                DGV.Refresh()
            End If

        ElseIf DGV.CurrentCell.EditedFormattedValue = Nothing Then
            If Not DGV.Rows(RowIndex).IsNewRow Then
                DGV.Rows.RemoveAt(RowIndex)
                DGV.Refresh()
            End If
        End If

I receive the following error 我收到以下错误

DGV.Rows.RemoveAt(0) {"Operation cannot be performed in this event handler."} 

I was try another process to this action. 我正在尝试另一种方法来执行此操作。 I try to simulate the KeyPress action by giving the following command 我尝试通过给出以下命令来模拟KeyPress动作

Dim myKeyData As System.Windows.Forms.Keys
Dim myMsg As System.Windows.Forms.Message
  myKeyData = Keys.Escape
ProcessCmdKey(myMsg, myKeyData)

But I receive the same error, when the pointer comes to execute the same instruction which executed when I press the Esc key. 但是,当指针执行与按Esc键时执行的指令相同的指令时,我会收到相同的错误。

您可以在事件处理程序完成之后,通过调用BeginInvoke在下一个消息循环中执行代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM