Articles in this section
Category / Section

How to restrict the removing of grouped columns from GroupDropArea when AllowRemove is enabled in WinForms GridGroupingControl?

2 mins read

GroupDropArea

To restrict the removing of grouped columns from GroupDropArea when AllowRemove is enabled for grouped columns, the CellClick event of GridGroupDropArea can be used.

In CellClick event, button bounds for remove option can be calculated and the removing of grouped columns can be restricted by disabling the clicking event using e.Cancel property when clicking bounds is on the remove button bounds.

C#

//Triggering the event.
this.gridGroupingControl1.GridGroupDropArea.CellClick += GridGroupDropArea_CellClick;
 
//Event Customization.
private void GridGroupDropArea_CellClick(object sender, Syncfusion.Windows.Forms.Grid.GridCellClickEventArgs e)
{
    GridGroupDropArea groupDropArea = sender as GridGroupDropArea;
    GridStyleInfo style = groupDropArea.Model[e.RowIndex, e.ColIndex];
    string cellValue = style.CellValue.ToString();
 
    Rectangle buttonBounds = ButtonArea(groupDropArea,e.RowIndex, e.ColIndex);
    if ((cellValue == "Description" || cellValue == "CategoryID") &&
        buttonBounds.Contains(e.MouseEventArgs.X, e.MouseEventArgs.Y))
    {
        e.Cancel = true;
    }
}
 
//Calculate the ButtonBounds for Remove button.
private Rectangle GetRemoveButtonBounds(GridGroupDropArea groupdropArea,int rowIndex,int colIndex)
{
    Rectangle cellBounds = groupdropArea.GetCellRenderer(rowIndex, colIndex).GetCellBoundsCore(rowIndex, colIndex);
    Rectangle innerBounds;
    Rectangle buttonsBounds;
    int buttonWidth = 8;
    innerBounds = cellBounds;
    Rectangle buttonArea = Rectangle.FromLTRB((((innerBounds.Right + 2 - (buttonWidth * 2)))), (innerBounds.Top - 1), (innerBounds.Right), innerBounds.Bottom);
    if (groupdropArea.GroupingControl.RightToLeft == System.Windows.Forms.RightToLeft.Yes)
    {
        buttonArea = Rectangle.FromLTRB(innerBounds.Left, (innerBounds.Top - 1), (((innerBounds.Left + (buttonWidth * 2)))), innerBounds.Bottom);
    }
    buttonsBounds = GridUtil.CenterInRect(buttonArea, new Size(buttonWidth, 20));
    return buttonsBounds;
}

 

VB

'Triggering the event.
AddHandler Me.gridGroupingControl1.GridGroupDropArea.CellClick, AddressOf GridGroupDropArea_CellClick
 
'Event Customization.
Private Sub GridGroupDropArea_CellClick(ByVal sender As Object, ByVal e As Syncfusion.Windows.Forms.Grid.GridCellClickEventArgs)
    Dim groupDropArea As GridGroupDropArea = TryCast(sender, GridGroupDropArea)
    Dim style As GridStyleInfo = groupDropArea.Model(e.RowIndex, e.ColIndex)
    Dim cellValue As String = style.CellValue.ToString()
 
    Dim buttonBounds As Rectangle = GetRemoveButtonBounds (groupDropArea,e.RowIndex, e.ColIndex)
    If (cellValue = "Description" OrElse cellValue = "CategoryID") AndAlso buttonBounds.Contains(e.MouseEventArgs.X, e.MouseEventArgs.Y) Then
        e.Cancel = True
    End If
End Sub
 
'Calculate the ButtonBounds for Remove button.
Private Function GetRemoveButtonBounds (ByVal groupdropArea As GridGroupDropArea, ByVal rowIndex As Integer, ByVal colIndex As Integer) As Rectangle
    Dim cellBounds As Rectangle = groupdropArea.GetCellRenderer(rowIndex, colIndex).GetCellBoundsCore(rowIndex, colIndex)
    Dim innerBounds As Rectangle
    Dim buttonsBounds As Rectangle
    Dim buttonWidth As Integer = 8
    innerBounds = cellBounds
    Dim buttonArea_Renamed As Rectangle = Rectangle.FromLTRB((((innerBounds.Right + 2 - (buttonWidth * 2)))), (innerBounds.Top - 1), (innerBounds.Right), innerBounds.Bottom)
    If groupdropArea.GroupingControl.RightToLeft = System.Windows.Forms.RightToLeft.Yes Then
        buttonArea_Renamed = Rectangle.FromLTRB(innerBounds.Left, (innerBounds.Top - 1), (((innerBounds.Left + (buttonWidth * 2)))), innerBounds.Bottom)
    End If
    buttonsBounds = GridUtil.CenterInRect(buttonArea_Renamed, New Size(buttonWidth, 20))
    Return buttonsBounds
End Function

 

Samples:

C#: Grouped Columns CS

VB: Grouped Columns VB

Reference link: https://help.syncfusion.com/windowsforms/gridgrouping/grouping

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