Creating a right click menu (context menu) on a datagridview

Posted in: Forms and Grids
By dePoPo
Mar 6, 2009 - 12:02:23 PM

In addition to the method below, you can also use the standard context menu provided by Visual Studio. The downside of the standard control is that it will not select the cell oyou are on when used with a datagrid. To get that behaviour you can trap the MouseDown event on the datagrid which is executed before the context menu control. So you could simply bind a standard context menu to the datagrid and this as handler:

        Private Sub DataGridView1_CellMouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles DataGridView1.CellMouseDown

        Try
            DataGridView1.CurrentCell = DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex)
        Catch ex As ArgumentException
            ' --- The user clicked outside a valid cell, we cant select
            Beep()
        End Try

    End Sub


The method below is an alternative method which has the same behaviour, but does not use the context menu control supplied by Visual Studio.

Additionaly, the method below dows not show the menu as a response to rigth clicks when the mouse is over a blank are of the grid.



Private Sub DataGridView1_CellMouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles DataGridView1.CellMouseUp
 
            If e.Button = Windows.Forms.MouseButtons.Right Then
 
              If e.ColumnIndex = -1 = False And e.RowIndex = -1 = False Then
                    ' Valid cell, Select it
                    Me.DataGridView1.ClearSelection()
                    Me.DataGridView1.CurrentCell = Me.DataGridView1.Item(e.ColumnIndex, e.RowIndex)
 
                    ' Popup the context menu
                    Dim mygrid As DataGridView = sender
                    Dim mymenu As ContextMenuStrip = New ContextMenuStrip
                    mymenu.Items.Add("Option 1", Nothing, AddressOf Option1Call)
                    mymenu.Items.Add("Option 2", Nothing, AddressOf Option2Call)
                    Dim mypoint As Point = mygrid.PointToClient(Control.MousePosition)
                    mymenu.Show(DataGridView1, mypoint.X, mypoint.Y)
                End If
            End If
 
        End Sub
 
 
        Public Sub Option1Call()
            Dim n_current_row As Integer
            Dim n_clicked_id As Integer
            Dim d_row As DataGridViewRow
            ' get the selected gridrow
            n_current_row = Me.DataGridView1.CurrentCell.RowIndex
            If n_current_row < 0 Then Exit Sub ' no data on this gridline
            d_row = DataGridView1.Rows(n_current_row)
            ' get the id value of the database record (assuming column 0 contains the id)
            n_clicked_id = d_row.Cells(0).Value
            MsgBox("Clicked database id: " & n_clicked_id)
        End Sub
 
 
        Public Sub Option2Call()
            MsgBox("Option 2 clicked")
        End Sub





Visitor Comments