Update custom summary on property changed

Hi,

I have custom summary group:

            <sfgrid:SfDataGrid.GroupSummaryRows>
              <sfgrid:GridGroupSummaryRow ShowSummaryInRow="True" Title="{}{PrincipalStageSummaryRow}">

                <sfgrid:GridGroupSummaryRow.SummaryColumns>

                  <sfgrid:GridSummaryColumn Name="PrincipalStageSummaryRow"
                                                MappingName="PrincipalStageSummary" <-- this is a property on my custom aggregator, which depends on another property in my data model -->
                                                CustomAggregate="{StaticResource customPrincipalStageAggregate}"
                                                Format="{}{PrincipalStageSummary}"
                                                SummaryType="Custom">
                  </sfgrid:GridSummaryColumn>

                </sfgrid:GridGroupSummaryRow.SummaryColumns>

              </sfgrid:GridGroupSummaryRow>
            </sfgrid:SfDataGrid.GroupSummaryRows> 

And I'd like to be able to update it once the property which the aggregator depends on changes - similar to how the Sum double aggregator works. Is there a simple way to be able to do this?

4 Replies

JA Jayaraman Ayyanar Syncfusion Team May 24, 2018 03:18 PM UTC

 
Thanks for contacting Syncfusion support. 
 
We have checked your query for using the custom aggregate in the custom summaries. You can achieve your requirement by Creating a custom aggregate class by deriving from ISummaryAggregate interface and in the CalculateAggregateFunc() method, you have to calculate the summary and assign it to the property. Refer the below code example. 
 
Code logics 
public class CustomAggregate : ISummaryAggregate 
{ 
    public CustomAggregate() 
    { 
    } 
    public double StdDev { get; set; } 
    public Action<System.Collections.IEnumerable, string, PropertyInfo> CalculateAggregateFunc() 
    { 
        return (items, property, pd) => 
        { 
            var enumerableItems = items as IEnumerable<OrderInfo>; 
            if (pd.Name == "StdDev") 
            { 
                this.StdDev = enumerableItems.StdDev<OrderInfo>(q => q.OrderID); 
            } 
        }; 
    } 
} 
 
public static class LinqExtensions 
{ 
    public static double StdDev<T>(this IEnumerable<T> values, Func<T, double?> selector) 
    { 
        double value = 0; 
        var count = values.Count(); 
        if (count > 0) 
        { 
            double? avg = values.Average(selector); 
            double sum = values.Select(selector).Sum(d => 
            { 
                if (d.HasValue) 
                { 
                    return Math.Pow(d.Value - avg.Value, 2); 
                } 
                return 0.0; 
            }); 
            value = Math.Sqrt((sum) / (count - 1)); 
        } 
        return value; 
    } 
} 
 
XAML logics 
<sfGrid:SfDataGrid.CaptionSummaryRow> 
    <sfGrid:GridSummaryRow Title="Standard Deviation:{CaptionSummary}" ShowSummaryInRow = "True">                                                      
        <sfGrid:GridSummaryRow.SummaryColumns> 
            <sfGrid:GridSummaryColumn Name="CaptionSummary" 
                                      CustomAggregate="{StaticResource customAggregate}" 
                                      Format="{}{StdDev}" 
                                      MappingName="OrderID" 
                                      SummaryType="Custom" /> 
        </sfGrid:GridSummaryRow.SummaryColumns> 
    </sfGrid:GridSummaryRow> 
</sfGrid:SfDataGrid.CaptionSummaryRow> 
 
You can also refer the same in our help documentation in the below link. 
 
Regards, 
Jayaraman. 



MV Malcolm van Staden May 25, 2018 07:47 AM UTC

Thank you for that information. It was, however, not what I was asking for.

I found the issue - I was using the incorrect mapping name:

MappingName="PrincipalStageSummary" <-- this is a property on my custom aggregator, which depends on another property in my data model -->

The MappingName needs to be the property on my data model, that way the NotifyPropertyChanged can be wired up correctly.


MV Malcolm van Staden May 25, 2018 10:09 AM UTC

Another very important note: 
If you need the summary to reflect any updates that are made to the property, the property which you base the summary on (the one which is specified by the MappingName) MUST be included in the grids columns even if it's just hidden (IsHidden=True) otherwise updates to the property will not be reflected automatically on the summary.

I'm going to assume that the grids data update mode needs to be correctly set as well:
myGrid.LiveDataUpdateMode = Syncfusion.Data.LiveDataUpdateMode.AllowDataShaping;


JA Jayaraman Ayyanar Syncfusion Team May 28, 2018 11:56 AM UTC

Hi Malcolm, 
 
We are glad that your issue has been resolved.  
 
We have checked your last updated query. The Summary does not refresh with data. To update the summary for the newly added row, or for the modified summary column, you need to set the SfDataGrid.View.LiveDataUpdateMode to LiveDataUpdateMode.AllowDataShaping or LiveDataUpdateMode.AllowSummaryUpdate. 
 
Please refer the below description for the options given to the LiveDataUpdateMode. 
 
Member 
Description 
AllowDataShaping 
Specifies that data operations sorting, grouping, filtering and summaries are updated based on data manipulation changes. 
AllowSummaryUpdate 
Specifies that summaries are updated based on data manipulation changes. 
Default 
Specifies that data operations are not updated during data manipulation. 
 
You can aslo refer the same in the API reference link below. 
 
Regards, 
Jayaraman. 


Loader.
Up arrow icon