locked
Data Grid View or multi column menu item in Context Menu RRS feed

  • Question

  • I am wondering if it is possible to host a datagridview or a multi column menu item within a context menu. 

    Here is what I am looking to do:

    Col1  Col2

    A        B     ---> Right click to get context menu -->    Col 3 Col4   
    C        D                                                                       E       F      --> right click to get context menu -->  Col5   Col6
                                                                                                                                                                      G        H

    or is it possible to do a multi column context menu strip?  

    At a loss of how to make this work...

    Thanks.

    Tuesday, July 12, 2016 8:47 PM

Answers

  •  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

    • Edited by IronRazerz Tuesday, July 12, 2016 11:06 PM
    • Marked as answer by DRC708 Wednesday, July 13, 2016 1:29 AM
    Tuesday, July 12, 2016 10:59 PM