Articles in this section
Category / Section

How to sort multiple columns without pressing Ctrl key in WinForms DataGrid (SfDataGrid)?

1 min read

WinForms DataGrid (SfDataGrid) allows you to perform the multiple sorting without pressing the Ctrl Key. You can achieve this by using the SortColumnsChanging event which will be raised while clicking on the column header to sort the column. You have to cancel the current sorting process and add the new sort column to the SorColumnDescriptions collection.

 

C#

public Form1()
{
    InitializeComponent();
    sfDataGrid1.DataSource = new OrderInfoCollection().OrdersListDetails;
    this.sfDataGrid1.SortColumnsChanging += OnSfDataGrid_SortColumnsChanging;
}
 
private void OnSfDataGrid_SortColumnsChanging(object sender, Syncfusion.WinForms.DataGrid.Events.SortColumnsChangingEventArgs e)
{
    e.Cancel = true;
 
    if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Add)
    {
        this.sfDataGrid1.BeginInvoke(new Action(() =>
        {
            this.sfDataGrid1.SortColumnDescriptions.Add(e.AddedItems[0]);
        }));
 
    }
    else if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Replace)
    {
        this.sfDataGrid1.BeginInvoke(new Action(() =>
        {
            var sortDescription = this.sfDataGrid1.SortColumnDescriptions.FirstOrDefault(sd => sd.ColumnName == e.AddedItems[0].ColumnName);
            this.sfDataGrid1.SortColumnDescriptions.Remove(sortDescription);
            this.sfDataGrid1.SortColumnDescriptions.Add(e.AddedItems[0]);
        }));
    }
}

 

MultiColumn Sorting

Take a moment to peruse the documentation, where you can find about Sorting in SfDataGrid, with code examples.

You can download the example from GitHub


Conclusion

I hope you enjoyed learning about how to sort multiple columns in WinForms DataGrid | Syncfusion.

You can refer to our WinForms DataGrid feature tour page to know about its other groundbreaking feature representations documentation

and how to quickly get started for configuration specifications.  You can also explore our WinForms DataGrid 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  to leave a comment
Access denied
Access denied