)
We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. (Last updated on: June 24, 2019).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to add ContextMenuStrip to the grid in WinForms GridControl and GridGroupingControl?

Platform: WinForms |
Control: GridGroupingControl |
Published Date: November 28, 2017 |
Last Revised Date: February 11, 2020
Tags: contextmenu

Add ContextMenuStrip to the grid

In order to show the ContextMenu in grid cells on right click, the ContextMenuStrip property of grid can be used and the ToolStripMenu items can be added to Items collection of ContextMenuStrip.

Initializing ContextMenu

C#

ContextMenuStrip contextMenuStrip1 = new ContextMenuStrip();
 
// copyToolStripMenuItem1
ToolStripMenuItem copyToolStripMenuItem1 = new ToolStripMenuItem();
this.copyToolStripMenuItem1.Name = "copyToolStripMenuItem1";
this.copyToolStripMenuItem1.Size = new System.Drawing.Size(175, 24);
this.copyToolStripMenuItem1.Text = "Copy";
 
//Adding toolstrip menu items to context menu
this.contextMenuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.copyToolStripMenuItem1,
this.editToolStripMenuItem,
this.findToolStripMenuItem});
this.contextMenuStrip1.Name = "contextMenuStrip1";
this.contextMenuStrip1.Size = new System.Drawing.Size(176, 104);

VB

Private contextMenuStrip1 As New ContextMenuStrip()
 
' copyToolStripMenuItem1
Dim copyToolStripMenuItem1 As New ToolStripMenuItem()
Me.copyToolStripMenuItem1.Name = "copyToolStripMenuItem1"
Me.copyToolStripMenuItem1.Size = New System.Drawing.Size(175, 24)
Me.copyToolStripMenuItem1.Text = "Copy"
 
'Adding toolstrip menu items to context menu
Me.contextMenuStrip1.Items.AddRange(New System.Windows.Forms.ToolStripItem() { Me.copyToolStripMenuItem1, Me.editToolStripMenuItem, Me.findToolStripMenuItem})
Me.contextMenuStrip1.Name = "contextMenuStrip1"
Me.contextMenuStrip1.Size = New System.Drawing.Size(176, 104)

Adding ContextMenu to grid

GridControl

C#

//Add the context menu to the grid
this.gridControl1.ContextMenuStrip = contextMenuStrip1;
 
//Event triggering
this.copyToolStripMenuItem1.Click += new System.EventHandler(this.copyToolStripMenuItem1_Click);
 
//Event Customization
private void copyToolStripMenuItem1_Click(object sender, EventArgs e)
{
    //Code to perform actions on context menu item click
    GridCurrentCell currentCell = this.gridControl1.CurrentCell;
    Clipboard.SetText(this.gridControl1[currentCell.RowIndex, currentCell.ColIndex].CellValue.ToString());
}

VB

'Add the context menu to the grid
Me.gridControl1.ContextMenuStrip = contextMenuStrip1
 
'Event triggering
AddHandler copyToolStripMenuItem1.Click, AddressOf copyToolStripMenuItem1_Click
 
'Event Customization
private void copyToolStripMenuItem1_Click(Object sender, EventArgs e)
    'Code to perform actions on context menu item click
    Dim currentCell As GridCurrentCell = Me.gridControl1.CurrentCell
    Clipboard.SetText(Me.gridControl1(currentCell.RowIndex, currentCell.ColIndex).CellValue.ToString())

GridGroupingControl

C#

//Adding ContextMenuStrip to the Grid.
this.gridGroupingControl1.ContextMenuStrip = contextMenuStrip1;
//Event Triggering
this.copyToolStripMenuItem.Click += new System.EventHandler(this.copyToolStripMenuItem_Click);
 
//Event Customization
private void copyToolStripMenuItem_Click(object sender, EventArgs e)
{
    //Code to perform actions on context menu item click
    int index = this.gridGroupingControl1.TableControl.GetClientCol(this.gridGroupingControl1.TableControl.CurrentCell.ColIndex);
    string columnName = this.gridGroupingControl1.TableDescriptor.Columns[index].Name;
    Record currentRecord = this.gridGroupingControl1.Table.CurrentRecord;
    Clipboard.SetText(currentRecord.GetValue(columnName).ToString());
}

VB

'Adding ContextMenuStrip to the Grid.
Me.gridGroupingControl1.ContextMenuStrip = contextMenuStrip1
 
'Event Triggering
AddHandler copyToolStripMenuItem.Click, AddressOf copyToolStripMenuItem_Click
 
'Event Customization
private void copyToolStripMenuItem_Click(Object sender, EventArgs e)
    'Code to perform actions on context menu item click
    Dim index As Integer = Me.gridGroupingControl1.TableControl.GetClientCol(Me.gridGroupingControl1.TableControl.CurrentCell.ColIndex)
    Dim columnName As String = Me.gridGroupingControl1.TableDescriptor.Columns(index).Name
    Dim currentRecord As Record = Me.gridGroupingControl1.Table.CurrentRecord
    Clipboard.SetText(currentRecord.GetValue(columnName).ToString())

Screenshot

GridControl

Show the contextmenustrip in grid

GridGroupingControl

Show the contextmenustrip in grid

Samples:

GridControl

C#: ContextMenuStrip_CS

VB: ContextMenuStrip_VB

GridGroupingControl

C#: ContextMenuStrip_CS

VB: ContextMenuStrip_VB

2X faster development

The ultimate WinForms UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Up arrow icon

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile
Live Chat Icon