I am working on DataGridView control in vb.net. I need help you that i want to delete a row in datagrid and only that row deleted who selected. Mean first i select the row then row deleted. So please provide me code that how i select and delete row from DataGridView control in VB.NET
thank
This question is related to
vb.net
winforms
datagridview
If dgv(11, dgv.CurrentRow.Index).Selected = True Then
dgv.Rows.RemoveAt(dgv.CurrentRow.Index)
Else
Exit Sub
End If
Assuming you are using Windows forms, you could allow the user to select a row and in the delete key click event. It is recommended that you allow the user to select 1 row only and not a group of rows (myDataGridView.MultiSelect = false)
Private Sub pbtnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
If myDataGridView.SelectedRows.Count > 0 Then
'you may want to add a confirmation message, and if the user confirms delete
myDataGridView.Rows.Remove(myDataGridView.SelectedRows(0))
Else
MessageBox.Show("Select 1 row before you hit Delete")
End If
End Sub
Note that this will not delete the row form the database until you perform the delete in the database.
Source: Stackoverflow.com