Sorting by aggregate values in FlexGrid

Posted by: rmenon on 1 November 2019, 11:22 am EST

    • Post Options:
    • Link

    Posted 1 November 2019, 11:22 am EST - Updated 4 October 2022, 12:03 am EST

    Hi,

    Is there a way to sort by aggregate values? i.e when a user clicks a column header in a grid with groupings, it looks like it sorts the underlying records, but doesn’t sort the aggregate value. In the attached screenshot, I am trying to sort by price.



    We are in the process of evaluating your WPF controls and this is something that our users would like to see.

  • Posted 3 November 2019, 8:19 pm EST

    Hello,

    The behavior you specified is by design. WPF FlexGrid uses CollectionView for features like Sorting/Grouping/Filtering and hence sorting is performed on the underlying data source.

    One way to work around it is to sort the underlying data source in the order of their group aggregates and update the FlexGrid.ItemsSource accordingly.

    private void FlexGrid_SortingColumn(object sender, C1.WPF.FlexGrid.CellRangeEventArgs e)
    {
     var source = flexGrid.ItemsSource as List<Product>;
     List<Product> sortedProducts = null;
     var groupDescriptions = flexGrid.CollectionView.GroupDescriptions;
    
     if(groupDescriptions.Count>0 && flexGrid.Columns[e.Column].BoundPropertyName == "Price")
     {
      if (_sortDirection == ListSortDirection.Ascending)
      {
       sortedProducts = source.GroupBy(x => x.Color).OrderBy(g => g.Sum(item => item.Price)).SelectMany(x => x).ToList();
       _sortDirection = ListSortDirection.Descending;
      }
      else if (_sortDirection == ListSortDirection.Descending)
      {
       sortedProducts = source.GroupBy(x => x.Color).OrderByDescending(g => g.Sum(item => item.Price)).SelectMany(x => x).ToList();
       _sortDirection = ListSortDirection.Ascending;
      }
    
      if (sortedProducts != null)
      {
       e.Cancel = true;
       flexGrid.ItemsSource = sortedProducts;
       foreach (var desc in groupDescriptions)
       {
        flexGrid.CollectionView.GroupDescriptions.Add(desc);
       }
       flexGrid.Columns["Price"].GroupAggregate = C1.WPF.FlexGrid.Aggregate.Sum;
      }
     }
    }
    

    Please refer to the attached sample for the same.

    prj_SortByAggregate.zip

    [NOTE: There are some limitations to this workaround. For example, Sort icon will not be displayed as we are sorting data on our own.]

    Regards,

    Basant

  • Posted 4 November 2019, 6:01 am EST

    Hi Basant,

    Thanks for replying. I understand the limitations, but that workaround is basically a no-go for us. We are displaying thousands of rows in our grid (with ticking data). Having to re-calc and reattach the datasource to the grid would be a pretty slow operation.

    Thanks,

    Ravi

  • Posted 4 November 2019, 6:23 pm EST

    Hi Ravi,

    I understand your concern and considering that I am discussing your case with the development team (Internal Tracking ID: 404387). So, I will inform you as soon as they suggest something.

    Thanks,

    Basant

  • Posted 4 November 2019, 9:15 pm EST

    Hi Ravi,

    According to the devs the workaround suggested earlier is the only possible way for sorting by aggregates. So, we are sorry for the inconvenience caused by the limitation.

    Regards

Need extra support?

Upgrade your support plan and get personal unlimited phone support with our customer engagement team

Learn More

Forum Channels