)
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 show ContextMenuStrip in a grid cell in the edit mode in WinForms GridControl?

Platform: WinForms |
Control: GridControl |
Published Date: April 30, 2011 |
Last Revised Date: December 19, 2019

Show context menu strip

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.

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