How to show the ContextMenuStrip to the grid in WinForms GridGroupingControl?
In order to show the ContextMenu in grid cells on right click, the ContextMenuStrip property of WinForms GridGroupingControl 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);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)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 Me.copyToolStripMenuItem.Click, AddressOf Me.copyToolStripMenuItem_Click
'Event Customization
Private Sub copyToolStripMenuItem_Click(ByVal sender As Object, ByVal e As EventArgs)
'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())
End Sub
Samples:
Conclusion
I hope you
enjoyed learning about how to add ContextMenuStrip to the grid in GridGroupingControl.
You can refer to our WinForms GridGroupingControl feature tour page to know about its other groundbreaking feature representations and WinForms GridGroupingControl 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!