Deleting Rows
To programmatically delete a row in the DataGridView control, you can use the Remove() method. The following code snippet removes all the selected rows in the DataGridView control:
Code:
For Each row As DataGridViewRow In DataGridView1.SelectedRows
DataGridView1.Rows.Remove(row)
Next
The user can also delete rows by first selecting the rows and then pressing the Delete key. By default, the deletion is done automatically without any prompting. But you may want to confirm the deletion with the user before deleting them. You can do so via the UserDeletingRow event:
Code:
Private Sub DataGridView1_UserDeletingRow( _
ByVal sender As Object, _
ByVal e As System.Windows.Forms. _
DataGridViewRowCancelEventArgs) _
Handles DataGridView1.UserDeletingRow
If (Not e.Row.IsNewRow) Then
Dim response As DialogResult = _
MessageBox.Show( _
"Are you sure you want to delete this row?", _
"Delete row?", _
MessageBoxButtons.YesNo, _
MessageBoxIcon.Question, _
MessageBoxDefaultButton.Button2)
If (response = DialogResult.No) Then
e.Cancel = True
End If
End If
End Sub
Bookmarks