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
If you are using a DevExpress gridcontrol, the code changes slightly, but still comes down to the same. After binding the standard context menu control to the gridcontrol, you can reference the currently selected row and retrive the value from a cell in it:
''' <summary>
''' this is the handler for the context menu's option
''' </summary>
''' <param name="sender"></param>
''' <param name="e"></param>
''' <remarks></remarks>
Private Sub BewerkenToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles BewerkenToolStripMenuItem.Click
' --- Retrieve the value from the ID column from the currently selected row
Dim row As System.Data.DataRow = GridView1.GetDataRow(GridView1.FocusedRowHandle)
MsgBox(row.Item("id"))
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: " &amp;amp; n_clicked_id)
End Sub
Public Sub Option2Call()
MsgBox("Option 2 clicked")
End Sub