Yes, you can add a DataGridView or any other control to a ContextMenuStrip. One way is to add a UserControl to your project and add the DataGridView to that. Then you can use ToolStripControlHost to host the UserControl in the
ContextMenu. Putting the DataGridView in a UserControl will allow you to easily set up the DGV in the designer window.
For example, i added a UserControl and added a DGV to it. Then in the Form designer i added a ContextMenu and used the code below.
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.ContextMenuStrip = ContextMenuStrip1
Dim uc1 As New UserControl1 'the UserControl with DGV on it
ContextMenuStrip1.Items.Add(New ToolStripControlHost(uc1))
End Sub
End Class
You could also skip the UserControl and just create a DataGridView dynamically in your code, set all the properties, and then host the DataGridView directly. This will be better if you need to have direct access to the DGV in your
Form`s code.
You can test it by adding a ContextMenuStrip to a new Form project and use the code below.
Public Class Form1
Private WithEvents dgv As New DataGridView
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.ContextMenuStrip = ContextMenuStrip1
'set up the DGV properties and add whatever rows you want in it
With dgv
.Width = 180
.Height = 80
.RowHeadersVisible = False
.AllowUserToAddRows = False
.Columns.Add("TheColumnName1", "Column 1")
.Columns.Add("TheColumnName2", "Column 2")
.Rows.Add(New Object() {"A", "B"})
.Rows.Add(New Object() {"C", "D"})
End With
ContextMenuStrip1.Items.Add(New ToolStripControlHost(dgv)) 'host the DGV in the main panel of the ContextMenuStrip
End Sub
End Class
If you say it can`t be done then i`ll try it