Sort grouped Grid by items count in ascending/descending order

Hi,
I'm reading data from a Datatable a bind it to SfDatagrid.Datasource grouping one Column.
Now I want to sort this Grid in descending order by items count.
The Group with most items on top and so on.

How can I do this ?

Thanks and best Regards
Helmut  

6 Replies 1 reply marked as answer

MA Mohanram Anbukkarasu Syncfusion Team June 8, 2021 01:01 PM UTC

Hi Helmut, 

Thanks for contacting Syncfusion support.  

SfDataGrid doesn’t have any direct support to achieve your requirement. However we are checking the possibilities to achieve this by using custom sorting support. We need two more working days to validate this. We will update with further details on June 10, 2021. We appreciate your patience until then. 

Regards, 
Mohanram A. 



HL Helmut Lubik June 8, 2021 01:38 PM UTC

Hi, Mohanram,

Thanks for your response.

I'm still waiting for a solution, and I think, this is an important feature to show a ranking grouped Grid.

Regards
Helmut


MA Mohanram Anbukkarasu Syncfusion Team June 9, 2021 01:34 PM UTC

Hi Helmut, 

Thanks for the update.  

As we mentioned in our previous update, there is no direct support to achieve your requirement. We will working on this. We will update with details on June 10 ,2021.  

Regards, 
Mohanram A. 



MA Mohanram Anbukkarasu Syncfusion Team June 10, 2021 11:10 AM UTC

Hi Helmut, 

Thanks for your patience.  

We have prepared a simple sample to achieve your requirement to sort the DataGrid based on the number of items in the groups by using the custom sorting support in SfDataGrid as shown in the following code example.  

Code example :  

public Form1() 
        { 
            InitializeComponent(); 
            sfDataGrid1.DataSource = new OrderInfoCollection().OrdersListDetails; 
 
            foreach (var column in this.sfDataGrid1.Columns) 
                this.sfDataGrid1.SortComparers.Add(new Syncfusion.Data.SortComparer() { Comparer = new CustomComparer(column.MappingName, this.sfDataGrid1), PropertyName = column.MappingName }); 
        } 
 
public class CustomComparer : IComparer<object>, ISortDirection 
{ 
    string ColumnName { get; set; } 
 
    SfDataGrid DataGrid { get; set; } 
    public CustomComparer(string columnName, SfDataGrid dataGrid) 
    { 
        this.ColumnName = columnName; 
        this.DataGrid = dataGrid; 
    } 
    public int Compare(object x, object y) 
    { 
        if (x.GetType() == typeof(OrderInfo)) 
        { 
            var valueX = this.DataGrid.View.GetPropertyAccessProvider().GetValue(x, ColumnName); 
            var valueY = this.DataGrid.View.GetPropertyAccessProvider().GetValue(y, ColumnName); 
            if (valueX is string && valueY is string) 
            { 
                if (valueX.ToString().CompareTo(valueY.ToString()) > 0) 
                    return SortDirection == ListSortDirection.Ascending ? 1 : -1; 
 
                else if (valueX.ToString().CompareTo(valueY.ToString()) == -1) 
                    return SortDirection == ListSortDirection.Ascending ? -1 : 1; 
                else 
                    return 0; 
            } 
            else if(valueX is DateTime && valueY is DateTime) 
            { 
                if ((DateTime.Parse(valueX.ToString())).CompareTo(DateTime.Parse(valueY.ToString())) > 0) 
                    return SortDirection == ListSortDirection.Ascending ? 1 : -1; 
 
                else if ((DateTime.Parse(valueX.ToString())).CompareTo(DateTime.Parse(valueY.ToString())) == -1) 
                    return SortDirection == ListSortDirection.Ascending ? -1 : 1; 
                else 
                    return 0; 
            } 
            else 
            { 
                if (double.Parse(valueX.ToString()) < double.Parse(valueY.ToString())) 
                    return SortDirection == ListSortDirection.Ascending ? 1 : -1; 
 
                else if (double.Parse(valueX.ToString()) > double.Parse(valueY.ToString())) 
                    return SortDirection == ListSortDirection.Ascending ? -1 : 1; 
                else 
                    return 0; 
            } 
        } 
 
        //While sorting groups 
        else if (x.GetType() == typeof(Group)) 
        {                      
            var countX = ((Group)x).ItemsCount; 
            var countY = ((Group)y).ItemsCount; 
 
            if (countX < countY) 
                return SortDirection == ListSortDirection.Ascending ? 1 : -1; 
 
            else if (countX > countY) 
                return SortDirection == ListSortDirection.Ascending ? -1 : 1; 
 
            else 
                return 0; 
        } 
        else 
            return 0; 
    } 
 
    private ListSortDirection _SortDirection; 
 
    /// <summary> 
    /// Gets or sets the property that denotes the sort direction. 
    /// </summary> 
    /// <remarks> 
    /// SortDirection gets updated only when sorting the groups. For other cases, SortDirection is always ascending. 
    /// </remarks> 
    public ListSortDirection SortDirection 
    { 
        get { return _SortDirection; } 
        set { _SortDirection = value; } 
    } 
} 



Please let us know if you require any other assistance from us.  

Regards, 
Mohanram A. 


Marked as answer

HL Helmut Lubik June 12, 2021 10:22 AM UTC

Thank you very much,

this is exactly what I need.

Best regards
Helmut


MA Mohanram Anbukkarasu Syncfusion Team June 14, 2021 03:37 AM UTC

Hi Helmut, 

Thanks for the update.   

We are glad to know that the provided solution worked at your end. Please let us know if you require further assistance from us. We are happy to help you.   

Regards,  
Mohanram A. 


Loader.
Up arrow icon