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.
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);
}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 SubSamples:
C#: ContextMenu-C#.
VB: ContextMenu-VB.
Conclusion
I hope you enjoyed learning how to show ContextMenuStrip in a grid cell in edit mode in WinForms GridControl.
You can refer to our WinForms GridControl feature tour page to know about its other groundbreaking feature representations and WinForms GridControl documentation, and how to quickly get started for configuration specifications.
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!