How to show ContextMenuStrip in a grid cell in the edit mode in WinForms GridControl?
Show context menu strip
In WinForms GridControl, by default, the ContextMenuStrip opens only on right click over the associated control. When the cell is in edit mode, it no longer receives mouse-related events. The control (TextBox or ComboBox) present in the cell alone receives the mouse events. So, to have a ContextMenuStrip in the edit mode, you need to set the ContextMenuStrip for those controls present in the cell. This can be achieved by handling the events CurrentCellControlGotFocus, CurrentCellControlLostFocus and MouseDown.
C#
void gridControl1_CurrentCellControlLostFocus(object sender, ControlEventArgs e) { e.Control.MouseDown -= new MouseEventHandler(Control_MouseDown); } void Control_MouseDown(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Right) this.contextMenuStrip1.Show(Control.MousePosition.X, Control.MousePosition.Y); } void gridControl1_CurrentCellControlGotFocus(object sender, ControlEventArgs e) { e.Control.MouseDown += new MouseEventHandler(Control_MouseDown); }
VB
Private Sub gridControl1_CurrentCellControlLostFocus(ByVal sender As Object, ByVal e As ControlEventArgs) RemoveHandler e.Control.MouseDown, AddressOf Control_MouseDown End Sub Private Sub Control_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs) If e.Button = MouseButtons.Right Then Me.contextMenuStrip1.Show(Control.MousePosition.X, Control.MousePosition.Y) End If End Sub Private Sub gridControl1_CurrentCellControlGotFocus(ByVal sender As Object, ByVal e As ControlEventArgs) AddHandler e.Control.MouseDown, AddressOf Control_MouseDown End Sub
Samples:
C#: ContextMenu-C#.
VB: ContextMenu-VB.
Conclusion
I hope you enjoyed learning how to show ContextMenuStrip in a grid cell in the edit mode in WinForms GridControl.
Refer to our WinForms GridControl’s feature tour page for its other groundbreaking feature representations. You can also explore our WinForms GridControl documentation to understand how to present and manipulate data.
For current customers, check out our WinForms components from the License and Downloads page. If you are new to Syncfusion, try our 30-day free trial to check out our WinForms GridControl and other WinForms components.
Please let us know in the following comment section if you have any queries or require clarifications. Contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!