Different ContextMenuStrip for DataGridView Cell, RowHeader and ColumnHeader

31,317

Solution 1

Use the DataGridView's MouseDown event to test if the right mouse has been clicked and if so use the associated HitTestInfo property to determine if a cell, row or column has been clicked. Use this information to display the ContextMenuStrip you need.

Here's an example MouseDown event that does this. To try the sample drop a DataGridView and three ContentMenuStrips on a form. Name the ContentMenuStrips mnuCell, mnuRow and mnuColumn.

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
        Dim ht As DataGridView.HitTestInfo
        ht = Me.DataGridView1.HitTest(e.X, e.Y)
        If ht.Type = DataGridViewHitTestType.Cell Then
            DataGridView1.ContextMenuStrip = mnuCell
            mnuCell.Items(0).Text = String.Format("This is the cell at {0}, {1}", ht.ColumnIndex, ht.RowIndex)
        ElseIf ht.Type = DataGridViewHitTestType.RowHeader Then
            DataGridView1.ContextMenuStrip = mnuRow
            mnuRow.Items(0).Text = "This is row " + ht.RowIndex.ToString()
        ElseIf ht.Type = DataGridViewHitTestType.ColumnHeader Then
            DataGridView1.ContextMenuStrip = mnuColumn 
            mnuColumn.Items(0).Text = "This is col " + ht.ColumnIndex.ToString()
        End If
    End If
End Sub

Here I'm assigning the DataGridView's ContextMenuStrip property to the ContextMenuStrip appropriate for the item right clicked (cell, row or column). To demonstrate how you might further customize the behavior of the ContextMenuStrips I'm also setting the text in each ContentMenuStrips' menu item.

Solution 2

On MouseDown event of DataGridView, use DataGridView.HitTest method to check what was clicked. Then you can switch context menus depending on what was clicked.

Share:
31,317
Smith
Author by

Smith

Am a software and website developer, i design graphics and icons too!. I love programming, although i only consider myself an intermediate programmer. Hobbies I do alot of googling and ask alot of questions. I love watching movies (Not violent), listening to music (soft, blues etc), reading Others Am very good with my hands (meaning i like to do constructive work with them). If you have a programming problem, pls don't hesitate to ask me for help, i will do the best i can. May you have peace!

Updated on February 24, 2020

Comments

  • Smith
    Smith about 4 years

    I want to set different ContextMenuStrip for DataGridView Cells, RowHeaders and ColumnHeaders.

    The idea is that when I right-click any of these items, a different ContextMenuStrip is displayed. How do I do this?

  • Smith
    Smith almost 13 years
    thanks, i have three contextmenu, one for row,column and cell. I placed the row contextmenu in this line ` "This is row "`, and disable the other two it still shows the one for , but it shows the row context menu when i click a cell.