I’ve got a DataGridView in a vb.net form, not connected to any source. I want to remove a row when the user pass to another row or focus out the DGV. The row should be removed if there’s an empty cell in it.
That’s my code:
Private Sub dgvSpSettings_RowLeave(sender As Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvSpSettings.RowLeave
If e.RowIndex < (dgvSpSettings.Rows.Count - 1) Then 'Esclude l'ultima riga dal controllo sulle righe vuote
If Me.dgvSpSettings.Rows(e.RowIndex).Cells(0).Value Is Nothing OrElse Me.dgvSpSettings.Rows(e.RowIndex).Cells(0).Value.ToString() = "" OrElse Me.dgvSpSettings.Rows(e.RowIndex).Cells(1).Value Is Nothing OrElse Me.dgvSpSettings.Rows(e.RowIndex).Cells(1).Value.ToString() = "" Then
Me.dgvSpSettings.Rows.RemoveAt(e.RowIndex)
End If
End If
End Sub
This doesn’t work because I can’t perform the method Me.dgvSpSettings.Rows.RemoveAt(e.RowIndex) in this event. I tried to store the row in a private var and then delete it in other events (like click), but the result is not what I expected.
Any suggestions?
That’s my final solution. It works for me and I hope it’s conceptually correct. I used delegates and Leave + LostFocus events.