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