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. Image for the cookie policy date
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to disable the contextmenu for specific row based on the column value in WinForms GridControl or GridGroupingControl?

Platform: WinForms |
Control: GridGroupingControl |
Published Date: September 21, 2018 |
Last Revised Date: February 11, 2020
Tags: contextmenu

Context menu

To display a context menu based on the cell value in grid, use the TableControl.MouseDown event. In this event, a specific cell value can be retrieved from the current record using the current cell style and setting the ContextMenu based on the cell value.

C#

//Event Subscription
 this.gridGroupingControl1.TableControl.MouseDown += TableControl_MouseDown;
 
//Event Customization
 private void TableControl_MouseDown(object sender, MouseEventArgs e)
 {
    if (e.Button == MouseButtons.Right)
    {
        int rowIndex, colIndex;
        this.gridGroupingControl1.TableControl.PointToRowCol(e.Location, out rowIndex, out colIndex);
        //To get the current cell style.
        GridTableCellStyleInfo style = this.gridGroupingControl1.TableControl.GetTableViewStyleInfo(rowIndex, colIndex);
        //To get the record.
        Record record = style.TableCellIdentity.DisplayElement.GetRecord();
        if(record != null)
        {
            bool value;
            string boolValue = record.GetValue("Boolean").ToString();
            if (bool.TryParse(boolValue, out value) && value)
                this.gridGroupingControl1.ContextMenu = null;
           else if (this.gridGroupingControl1.ContextMenu == null)
                this.gridGroupingControl1.ContextMenu = menu;
        }
        else
        {
            //To avoid the contextmenu for header row.
            this.gridGroupingControl1.ContextMenu = null;
        }
    }
}

VB

'Event Subscription
AddHandler Me.gridGroupingControl1.TableControl.MouseDown, AddressOf TableControl_MouseDown
 
'Event Customization
Private Sub TableControl_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs)
   If e.Button = MouseButtons.Right Then
       Dim rowIndex, colIndex As Integer
       Me.gridGroupingControl1.TableControl.PointToRowCol(e.Location, rowIndex, colIndex)
       'To get the current cell style.
       Dim style As GridTableCellStyleInfo = Me.gridGroupingControl1.TableControl.GetTableViewStyleInfo(rowIndex, colIndex)
       'To get the record.
       Dim record As Record = style.TableCellIdentity.DisplayElement.GetRecord()
       If record IsNot Nothing Then
          Dim value As Boolean
          Dim boolValue As String = record.GetValue("Boolean").ToString()
          If Boolean.TryParse(boolValue, value) AndAlso value Then
  Me.gridGroupingControl1.ContextMenu = Nothing
          ElseIf Me.gridGroupingControl1.ContextMenu Is Nothing Then
   Me.gridGroupingControl1.ContextMenu = menu_Renamed
          End If
          Else
  'To avoid the contextmenu for header row.
  Me.gridGroupingControl1.ContextMenu = Nothing
          End If
       End If
End Sub

Screenshot

Show the context menu

Samples:

C#: Disable contextmenu on Booleanvalue_CS

VB: Disable contextmenu on Booleanvalue_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