How to add ContextMenuStrip to the grid in WinForms GridGroupingControl?
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
GridGroupingControl
Samples:
GridControl
GridGroupingControl
Conclusion
I hope you enjoyed learning about how to add ContextMenuStrip to the grid in WinForms GridGroupingControl.
You can refer to our WinForms GridGroupingControl feature
tour
page to know about its other groundbreaking feature
representations and documentation, and how to quickly get started for configuration
specifications. You can also explore our WinForms GridGroupingControl example to understand how to create and manipulate data.
For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.
If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!