Results 1 to 9 of 9

Thread: [RESOLVED] Delete DataGridView row after menu click

Threaded View

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2000
    Location
    Minnesota
    Posts
    830

    [RESOLVED] Delete DataGridView row after menu click

    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
    Last edited by lleemon; Aug 28th, 2012 at 07:53 AM. Reason: Resolved

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width