Articles in this section
Category / Section

How to disable the contextmenu for specific row in WinForms GridGroupingControl?

2 mins read

Context menu

To display a context menu based on the cell value in WinForms GridGrouping Control, 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

Conclusion

I hope you enjoyed learning about how to disable the contextmenu for specific row in WinForms GridGroupingControl.

You can refer to our WinForms GridGroupingControl feature tour page to know about its other groundbreaking feature representations and 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 forumsDirect-Trac, or feedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied