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 N开发者_如何学编程ot 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.
You can execute code in the next message loop, after the event handler finishes, by calling BeginInvoke
.
精彩评论