If user would like to edit some data in DataGridView he might remove selected row. DataGridView is a control to show data from database.
Attention! This post has been published on my old website that has been down. I’m archiving old not verified posts for possible future use. Please be careful when using this post content.
In following code selected row is removing from DataGridView2 this my control name. Remove buttom is hidden in MenuStrip:
Private Sub UusnItemToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles UusnItemToolStripMenuItem.Click ' Removing selected row from dgv2 For Each row As DataGridViewRow In DataGridView2.SelectedRows DataGridView2.Rows.Remove (Row) Next End Sub
There is possiblity to extend code to ask user to confirm decision while he wants to remove selected row:
Private Sub UsuńZaznaczonyProduktToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles UsuńZaznaczonyProduktToolStripMenuItem.Click ' Removing product's row in DataGridView from products' list Dim i, j, TakNie As Integer j = DataGridView2.CurrentRow.Index i = DataGridView2.Rows.Count TakNie = MsgBox("Would you like to remove " & DataGridView2.Item(0, j).Value & " from order?" & _ Chr(10) & Chr(10) & "Continue?", vbYesNo + vbQuestion, "APP QUESTION...") If TakNie = MsgBoxResult.Yes Then Try For Each row As DataGridViewRow In DataGridView2.SelectedRows DataGridView2.Rows.Remove (Row) Next Catch ex As Exception MsgBox("Error happened." + Chr(10) + Chr(10) + "Err no.:" + Chr(10) + ex.Message, _ MsgBoxStyle.Critical, "ORDER CLEARING......") End Try Else ' Nothing happened, user chosen CANCEL button End If End Sub