Question

I have requirement to sort group summary field.

Ex. I have 3 columns in the grid.

Step 1 : I have group by Id by dragging the Id column in Group by area.

Step 2: Add Sum,Count,Average on column.

Now i want to sort sum or count or average by clicking on that ,so that the whole grouped is sorted by sum like 100,200,300.

please help

enter image description here

Was it helpful?

Solution

The sort order is controlled by the GroupByComparer of the FieldSettings class and this can be accomplished by creating a custom IComparer for the field that is grouped. Note that grouping is actually also a sort so I am going to assume that you still want the default sort to happen when the column is first grouped.

In the following example group by records can be sorted by a single summary result when it is clicked on. This was accomplished by using a custom IComparer for the groups that sorts by the value of the tag if it is set and if not set falls back to the value of the group by record:

public class SummarySortComparer : IComparer
{
    public int Compare(object x, object y)
    {
        GroupByRecord xRecord = x as GroupByRecord;
        GroupByRecord yRecord = y as GroupByRecord;
        IComparable xValue = xRecord.Value as IComparable;
        object yValue = yRecord.Value;
        if (xRecord.Tag != null)
        {
            xValue = xRecord.Tag as IComparable;
            yValue = yRecord.Tag;
        }            
        return xValue.CompareTo(yValue);
    }
}

This is set on the grid using the following:

this.XamDataGrid1.FieldSettings.GroupByComparer = new SummarySortComparer();

Use the PreviewMouseLeftButtonDown of the grid to get the summary that was clicked on if there was one and set the tag of the group by records to be the value of that summary and refresh the sort of the grid:

void XamDataGrid1_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
    SummaryResultPresenter summaryResultPresenter =
        Utilities.GetAncestorFromType(e.OriginalSource as DependencyObject, typeof (SummaryResultPresenter), false) as
            SummaryResultPresenter;
    if (summaryResultPresenter != null)
    {
        GroupBySummariesPresenter groupBySummariesPresenter =
            Utilities.GetAncestorFromType(summaryResultPresenter,
                typeof(GroupBySummariesPresenter), false) as GroupBySummariesPresenter;
        if (groupBySummariesPresenter != null)
        {
            SummaryResult summaryResult = summaryResultPresenter.SummaryResult;
            int summaryResultIndex = summaryResult.ParentCollection.IndexOf(summaryResult);

            foreach (GroupByRecord groupRecord in groupBySummariesPresenter.GroupByRecord.ParentCollection)
            {
                groupRecord.Tag = groupRecord.ChildRecords.SummaryResults[summaryResultIndex].Value;
            }

            this.XamDataGrid1.Records.RefreshSort();
        }
    }
}

Note that there are a few limitations in this example in that I haven't implemented any way to clear what summary is sorted so that is something that if desired would still need to be implemented by you. I also didn't include logic to change the sort direction and used the direction that the field is currently sorted by so if you also want to update the direction this will need to be added as well.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top