简体   繁体   中英

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.

The general idea of this process is when all the cells are fulfilled then

  • Write the Data into a SQL table and
  • 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). 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

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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