DYNAMICALLY SHOW CONTEXTMENUSTRIP FROM DATAGRIDVIEW

emi_sastra
emi_sastra used Ask the Experts™
on
Hi All,

I show a windows form datagridview, it is full selected row.
When use click a row, I want to show contextmenustrip at that clicked point.

How could I do it ?

Thank you.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Author

Commented:
Revise.

I show a windows form datagridview, it is full selected row.
When user right click a row, I want to show contextmenustrip at that clicked point.
Commented:
Add a contextmenu  to the datagridview (either in code or through the designer) then handle the mousedown event of the datagridview and from there show the contextmenu using the location clicked, e.g
Private Sub DataGridView1_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles DataGridView1.MouseDown
        If e.Button = Windows.Forms.MouseButtons.Right Then
            DataGridView1.ContextMenu.Show(DataGridView1, e.Location)
        End If
End Sub

Open in new window

or call the contextmenu to show by
Private Sub DataGridView1_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles DataGridView1.MouseDown
        If e.Button = Windows.Forms.MouseButtons.Right Then
            xMenu.Show(DataGridView1, e.Location)
        End If
End Sub

Open in new window

You can add a context menu with items in code by:
Dim xMenu As New ContextMenu
xMenu.MenuItems.AddRange({New MenuItem With {.Text = "Item 1"}, New MenuItem With {.Text = "Item 2"}})
DataGridView1.ContextMenu = xMenu

Open in new window

where DataGridView1 is your datagridview

Author

Commented:
Hi nepaluz,

It works.

Thank you very much for your help.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial