Articles in this section
Category / Section

How to use your own context menu along with the FieldChooser option in WinForms GridGroupingControl?

1 min read

Description:

By default, the FieldChooser context menu is displayed when you right-click on the ColumnHeadercell of the Grid but it is not displayed when you right-click on the Grid cells.


C#

//declared outside of the form constructor for public access
FieldChooser fieldchooser;

//In Form constructor
fieldchooser = new FieldChooser(this.gridGroupingControl1);

VB

'declared outside of the form constructor for public access
Dim fieldchooser As FieldChooser

'In Form constructor
fieldchooser = New FieldChooser(Me.gridGroupingControl1)

When you try to use your own context menu along with the FieldChooser context menu item, it overrides the FieldChooser context menu.


Solution:

The context menu that is displaying when you right-click the ColumnHeadercell can be restricted by the TableControlMouseDown event. By using this event, you can customize ContextMenuStrip to display only in the cells other than the header cells.

C#

// User-defined context menu
contextMenuStrip = new ContextMenuStrip();
contextMenuStrip.Items.Add("Copy");
contextMenuStrip.Items.Add("Paste");

// Handle context menu item click
contextMenuStrip.ItemClicked += new ToolStripItemClickedEventHandler(contextMenuStrip_ItemClicked);

// Handle mouse down event on the grid
this.gridGroupingControl1.TableControlMouseDown += gridGroupingControl1_TableControlMouseDown;

// Event handler for context menu item click
void contextMenuStrip_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
{
    if (e.ClickedItem.Text == "Copy")
    {
        int count = this.gridGroupingControl1.Table.SelectedRecords.Count;
        MessageBoxAdv.Show(count.ToString());
    }
}

// Event handler for mouse down on grid table control
void gridGroupingControl1_TableControlMouseDown(object sender, GridTableControlMouseEventArgs e)
{
    int row = 0, col = 0;
    this.gridGroupingControl1.TableControl.PointToRowCol(e.Inner.Location, out row, out col);
    GridTableCellStyleInfo style = this.gridGroupingControl1.TableControl.GetTableViewStyleInfo(row, col);

    // Check whether it is a column header cell and right-click
    if (style != null &&
        style.TableCellIdentity.TableCellType == GridTableCellType.ColumnHeaderCell &&
        e.Inner.Button == System.Windows.Forms.MouseButtons.Right)
    {
        gridGroupingControl1.ContextMenuStrip = fieldchooser.ContextMenu;
    }
    else // If it is not a column header cell
    {
        gridGroupingControl1.ContextMenuStrip = contextMenuStrip;
    }
}

VB

'User-defined context menu
contextMenuStrip = New ContextMenuStrip()
contextMenuStrip.Items.Add("Copy")
contextMenuStrip.Items.Add("Paste")

'Handle context menu item click
AddHandler contextMenuStrip.ItemClicked, AddressOf contextMenuStrip_ItemClicked

'Handle mouse down event on the grid
AddHandler Me.gridGroupingControl1.TableControlMouseDown, AddressOf gridGroupingControl1_TableControlMouseDown

'Event handler for context menu item click
Private Sub contextMenuStrip_ItemClicked(sender As Object, e As ToolStripItemClickedEventArgs)
    If e.ClickedItem.Text = "Copy" Then
        Dim count As Integer = Me.gridGroupingControl1.Table.SelectedRecords.Count
        MessageBoxAdv.Show(count.ToString())
    End If
End Sub

'Event handler for mouse down on grid table control
Private Sub gridGroupingControl1_TableControlMouseDown(sender As Object, e As GridTableControlMouseEventArgs)
    Dim row As Integer = 0, col As Integer = 0
    Me.gridGroupingControl1.TableControl.PointToRowCol(e.Inner.Location, row, col)
    Dim style As GridTableCellStyleInfo = Me.gridGroupingControl1.TableControl.GetTableViewStyleInfo(row, col)

    'Check whether it is a column header cell and right-click
    If style IsNot Nothing AndAlso
       style.TableCellIdentity.TableCellType = GridTableCellType.ColumnHeaderCell AndAlso
       e.Inner.Button = Windows.Forms.MouseButtons.Right Then

        gridGroupingControl1.ContextMenuStrip = fieldchooser.ContextMenu
    Else
        'If it is not a column header cell
        gridGroupingControl1.ContextMenuStrip = contextMenuStrip
    End If
End Sub

The following screenshot displays the context menu.



Sample links:

C#: Contextmenu

VB: Contextmenu

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied