I have a DataGridView that gets populated from another action. I now want to add functionality where the user right clicks on a select row and a menu appears. If they click on the menu item 'Delete', to delete the row they right click on. I am not sure I know how to get a handle on this row and delete.
What do I do in DeleteRow to delete the specific row clicking on?
Code:.... Dim contextMenuForColumn1 As ContextMenu = New ContextMenu() ... Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load 'Add all default values of controls contextMenuForColumn1.MenuItems.Add("Delete", AddressOf Me.DeleteRow) '... other menu items End Sub ... Sub DeleteRow() Dim iRow As Integer = 0 While iRow <= dgvItems.Rows.Count - 1 If dgvItems.Rows(iRow).Cells(0).Selected = True Then MessageBox.Show(dgvItems.Rows(iRow).Cells(0).Value) End If iRow += 1 End While End Sub Private Sub dgvItems_MouseUp(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles dgvItems.MouseUp Dim hitTestInfo As DataGridView.HitTestInfo If e.Button = Windows.Forms.MouseButtons.Right Then hitTestInfo = dgvItems.HitTest(e.X, e.Y) If hitTestInfo.Type = DataGridViewHitTestType.Cell Or hitTestInfo.ColumnIndex = 0 Then contextMenuForColumn1.Show(dgvItems, New Point(e.X, e.Y)) End If End If End Sub




Reply With Quote