Articles in this section
Category / Section

How to sort column by double clicking on column header in WinForms GridGroupingControl?

2 mins read

Sorting

By default, sorting a column will be performed in single click on column header of the WinForms GridGroupingControl. In order to perform the column sorting on double clickTableControlMouseDown event can be customized. To restrict the sorting in single click, enable e.Cancel property in TableControlCellClick event.


Enabling sorting on double click


C#

//Event Triggering
gridGroupingControl1.TableControlMouseDown += new GridTableControlMouseEventHandler(gridGroupingControl1_TableControlMouseDown);

//Event Customization
private void gridGroupingControl1_TableControlMouseDown(object sender, GridTableControlMouseEventArgs e)
{
    if (e.Inner.Clicks == 2)
    {
        int rowIndex = 0;
        int colIndex = 0;
        e.TableControl.PointToRowCol(e.Inner.Location, out rowIndex, out colIndex);
        GridTableCellStyleInfo style = e.TableControl.GetTableViewStyleInfo(rowIndex, colIndex);
        Element el = style.TableCellIdentity.DisplayElement;
 
        if ((el is ColumnHeaderRow || el is ColumnHeaderSection) && el.ParentTable == this.gridGroupingControl1.Table)
        {
            GridColumnDescriptor column = style.TableCellIdentity.Column;
 
            bool ctrlKeyPressed = this.gridGroupingControl1.TableDescriptor.TableOptions.AllowMultiColumnSort && (Control.ModifierKeys & Keys.Control) != 0;
 
            SortColumnDescriptor sd = this.gridGroupingControl1.TableDescriptor.GroupedColumns[column.MappingName];
 
            //Sorting the column if that column in groups.
            if (sd != null)
            {
                if (sd.SortDirection == ListSortDirection.Ascending)
                {
                    sd.SortDirection = ListSortDirection.Descending;
                }
                else
                {
                    sd.SortDirection = ListSortDirection.Ascending;
                }
            }
            else
            {
                sd = this.gridGroupingControl1.TableDescriptor.SortedColumns[column.MappingName];
 
                if (sd != null && (this.gridGroupingControl1.TableDescriptor.SortedColumns.Count == 1 || ctrlKeyPressed))
                {
                    if (sd.SortDirection == ListSortDirection.Ascending)
                    {
                        sd.SortDirection = ListSortDirection.Descending;
                    }
                    else
                    {
                        sd.SortDirection = ListSortDirection.Ascending;
                    }
                }
                //To add multiple column sorting
                else if (ctrlKeyPressed)
                {
                    this.gridGroupingControl1.TableDescriptor.SortedColumns.Add(column.MappingName);
                }
                else
                {
                    this.gridGroupingControl1.TableDescriptor.SortedColumns.Clear();
                    this.gridGroupingControl1.TableControl.BeginUpdate(BeginUpdateOptions.Invalidate);
                    this.gridGroupingControl1.TableDescriptor.SortedColumns.Add(column.MappingName);
                    this.gridGroupingControl1.TableControl.EndUpdate(true);
                }
            }
        }
    }
}
VB
'Event Triggering
AddHandler() gridGroupingControl1.TableControlMouseDown, AddressOf gridGroupingControl1_TableControlMouseDown

'Event Customization
Private Sub gridGroupingControl1_TableControlMouseDown(ByVal sender As Object, ByVal e As GridTableControlMouseEventArgs)
    If e.Inner.Clicks = 2 Then
        Dim rowIndex As Integer = 0
        Dim colIndex As Integer = 0
        e.TableControl.PointToRowCol(e.Inner.Location, rowIndex, colIndex)
        Dim style As GridTableCellStyleInfo = e.TableControl.GetTableViewStyleInfo(rowIndex, colIndex)
        Dim el As Element = style.TableCellIdentity.DisplayElement
        If (TypeOf el Is ColumnHeaderRow OrElse TypeOf el Is ColumnHeaderSection) AndAlso el.ParentTable Is Me.gridGroupingControl1.Table Then
            Dim column As GridColumnDescriptor = style.TableCellIdentity.Column
            Dim ctrlKeyPressed As Boolean = Me.gridGroupingControl1.TableDescriptor.TableOptions.AllowMultiColumnSort AndAlso (Control.ModifierKeys And Keys.Control) <> 0
            Dim sd As SortColumnDescriptor = Me.gridGroupingControl1.TableDescriptor.GroupedColumns(column.MappingName)

            'Sorting the column if that column in groups.
            If sd IsNot Nothing Then
                If sd.SortDirection = ListSortDirection.Ascending Then
                    sd.SortDirection = ListSortDirection.Descending
                Else
                    sd.SortDirection = ListSortDirection.Ascending
                End If
            Else
                sd = Me.gridGroupingControl1.TableDescriptor.SortedColumns(column.MappingName)
                If sd IsNot Nothing AndAlso (Me.gridGroupingControl1.TableDescriptor.SortedColumns.Count = 1 OrElse ctrlKeyPressed) Then
                    If sd.SortDirection = ListSortDirection.Ascending Then
                        sd.SortDirection = ListSortDirection.Descending
                    Else
                        sd.SortDirection = ListSortDirection.Ascending
                    End If
                'To add multiple column sorting
                ElseIf ctrlKeyPressed Then
                    Me.gridGroupingControl1.TableDescriptor.SortedColumns.Add(column.MappingName)
                Else
                    Me.gridGroupingControl1.TableDescriptor.SortedColumns.Clear()
                    Me.gridGroupingControl1.TableControl.BeginUpdate(BeginUpdateOptions.Invalidate)
                    Me.gridGroupingControl1.TableDescriptor.SortedColumns.Add(column.MappingName)
                    Me.gridGroupingControl1.TableControl.EndUpdate(True)
                End If
            End If
        End If
    End If
End Sub

Disabling sorting on single click


C#

//Event Triggering
gridGroupingControl1.TableControlCellClick += new GridTableControlCellClickEventHandler(gridGroupingControl1_TableControlCellClick);

//Event Customization
private void gridGroupingControl1_TableControlCellClick(object sender, GridTableControlCellClickEventArgs e)
{
    GridTableCellStyleInfo style = e.TableControl.GetTableViewStyleInfo(e.Inner.RowIndex, e.Inner.ColIndex);
    Element el = style.TableCellIdentity.DisplayElement;
 
    if ((el is ColumnHeaderRow || el is ColumnHeaderSection) && el.ParentTable == this.gridGroupingControl1.Table)
    {
        // To disable the sorting on single click
        e.Inner.Cancel = true;
     }
}

VB

'Event Triggering
AddHandler gridGroupingControl1.TableControlCellClick, AddressOf gridGroupingControl1_TableControlCellClick

'Event Customization
Private Sub gridGroupingControl1_TableControlCellClick(ByVal sender As Object, ByVal e As GridTableControlCellClickEventArgs)
    Dim style As GridTableCellStyleInfo = e.TableControl.GetTableViewStyleInfo(e.Inner.RowIndex, e.Inner.ColIndex)
    Dim el As Element = style.TableCellIdentity.DisplayElement
    If (TypeOf el Is ColumnHeaderRow OrElse TypeOf el Is ColumnHeaderSection) AndAlso el.ParentTable Is Me.gridGroupingControl1.Table Then
        'To disable the sorting on single click
        e.Inner.Cancel = True
    End If
End Sub

Samples:

C#: SortingOnDoubleClick_CS

VB: SortingOnDoubleClick_VB


Reference Link: Sorting

Conclusion


I hope you enjoyed learning about how to sort column by double clicking on column header in GridGroupingControl.


You can refer to our  WinForms GridGroupingControl feature tour page to know about its other groundbreaking feature representations. You can also explore our WinForms GridGroupingControl documentation 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  to leave a comment
Access denied
Access denied