Summary Calculation

Hello,

I want to know how to manipulate a specific column in a SummaryRow, right now I something like this:

Columns:
Name | Last Month | Current Month | % Sales

summaryRow.SummaryColumns.Add(new GridSummaryColumn()
            {
                Name = "Last Month",
                MappingName = "LastMonth",
                Format = "{Sum:C}",
                SummaryType = SummaryType.DoubleAggregate,
            });

summaryRow.SummaryColumns.Add(new GridSummaryColumn()
            {
                Name = "Current Month",
                MappingName = "CurrentMonth",
                Format = "{Sum:C}",
                SummaryType = SummaryType.DoubleAggregate
            });

The third column (% Sales) uses the next formula (CurrentMonth * 100 / LastMonth) - 100
How can I create this column in the SummaryRow?

3 Replies

SS Sivaraman Sivagurunathan Syncfusion Team January 19, 2018 12:58 PM UTC

Hi Carlos, 
 
We have checked your query. You can achieve your requirement by using Custom Summaries in our SfDataGrid. You can pass your column value as parameter of custom summary calculation method with help of ISummaryAggregate.CalculateAggregateFunc method.  
 
We have created a sample based on your requirement and attached the same for your reference which can be downloaded from the below link. 
 
 
 
Refer the UG Link for more details:  
 
 
The below code illustrates how to using custom summaries. 
 
// xaml Code 
 
<syncfusion:SfDataGrid.CaptionSummaryRow> 
<syncfusion:GridSummaryRow Title="LastMonth {TotalSalary} CurrentMonth {ProductCount} and Avg {Sale_Percentage}" 
                        ShowSummaryInRow="True"> 
  <syncfusion:GridSummaryRow.SummaryColumns> 
    <syncfusion:GridSummaryColumn Name="TotalSalary" 
                              Format="{}{Sum:c}" 
                              MappingName="OrderID" 
                              SummaryType="DoubleAggregate" /> 
    <syncfusion:GridSummaryColumn Name="ProductCount" 
                              Format="{}{Sum:c}" 
                              MappingName="EmployeeID" 
                              SummaryType="DoubleAggregate" /> 
    <syncfusion:GridSummaryColumn Name="Sale_Percentage" 
                    CustomAggregate="{StaticResource customAggregate}" 
                    Format="'{_Percentage:c}'" 
                    MappingName="Freight" 
                    SummaryType="Custom" /> 
  </syncfusion:GridSummaryRow.SummaryColumns> 
      </syncfusion:GridSummaryRow> 
    </syncfusion:SfDataGrid.CaptionSummaryRow> 
 
//Code Behind   
 
public class CustomAggregate : ISummaryAggregate 
{ 
    public CustomAggregate() 
    { 
    } 
    public double _Percentage { get; set; } 
 
    Action<IEnumerable, string, PropertyInfo> ISummaryAggregate.CalculateAggregateFunc() 
    { 
        return (items, property, pd) => 
        { 
            var enumerableItems = items as IEnumerable<OrderInfo>; 
            if (pd.Name == "_Percentage") 
            { 
            // Pass your column name here  
            this._Percentage = enumerableItems.Percentage<OrderInfo>(q => q.OrderID, q => q.EmployeeID); 
            } 
        }; 
    } 
} 
 
public static class LinqExtensions 
{ 
    public static double Percentage<T>(this IEnumerable<T> values, Func<T, double> _value1, Func<T, double> _value2) 
    { 
        double average = 0; 
        var count = values.Count(); 
        if (count > 0) 
        { 
            double value1 = values.Sum(_value1); 
            double value2 = values.Sum(_value2); 
            average = ((value1 * 100) / value2) - 100; 
            return average; 
        } 
        return average; 
    } 
} 
 
 
 
Regards, 
Sivaraman  
 



CS Carlos Salazar January 19, 2018 10:23 PM UTC

Hello Sivaraman,

Thanks for the example, I think I wasn't clear with my question, please find attached an excel file with an example of the table I want to draw in my app
In the file you can see the formulas that must be used in order to calculate the summary row, the % column is already calculated by my backend.

Thanks in advance.

Attachment: example_478b153d.zip


SS Sivaraman Sivagurunathan Syncfusion Team January 22, 2018 10:11 AM UTC

Hi Carlos,   
   
Thanks for the update.   
   
We have checked the attached excel document. You can achieve your requirement by using GridUnboundColumn.Expression property and GridSummaryColumn.Format property. We have prepared a sample for your reference and you can download the same from the below sample link.    
   
Also, refer the below UG links for more details:   
   
The below code illustrates how to use GridUnboundColumn and GridSummaryColumn.   
   
   
<syncfusion:SfDataGrid.Columns >   
  <syncfusion:GridTextColumn MappingName="FirstName" HeaderText="Name" />   
  <syncfusion:GridTextColumn HeaderText="Last Month" MappingName="OrderID" />   
  <syncfusion:GridTextColumn HeaderText="Current Month" MappingName="EmployeeID" />   
  <syncfusion:GridUnboundColumn HeaderText="Sale %" MappingName="Unbound" Expression="(((EmployeeID*100)/OrderID)-100)"/>   
  <syncfusion:GridTextColumn HeaderText="Ship Country" MappingName="ShipCountry" />   
</syncfusion:SfDataGrid.Columns>   
<syncfusion:SfDataGrid.GroupColumnDescriptions>   
  <syncfusion:GroupColumnDescription ColumnName="FirstName" />   
</syncfusion:SfDataGrid.GroupColumnDescriptions>   
<syncfusion:SfDataGrid.CaptionSummaryRow>   
  <syncfusion:GridSummaryRow Title="LM {TotalSalary} CM {ProductCount} and Avg {Sale_Percentage}"   
                              ShowSummaryInRow="True">   
    <syncfusion:GridSummaryRow.SummaryColumns>   
      <syncfusion:GridSummaryColumn Name="TotalSalary"   
                                    Format="{}{Sum:c}"   
                                    MappingName="OrderID"   
                                    SummaryType="DoubleAggregate" />   
      <syncfusion:GridSummaryColumn Name="ProductCount"   
                                    Format="{}{Sum:c}"   
                                    MappingName="EmployeeID"   
                                    SummaryType="DoubleAggregate" />   
      <syncfusion:GridSummaryColumn Name="Sale_Percentage"   
                                    Format="{}{Average:c}"   
                                    MappingName="Unbound"   
                                    SummaryType="DoubleAggregate" />   
    </syncfusion:GridSummaryRow.SummaryColumns>   
  </syncfusion:GridSummaryRow>   
</syncfusion:SfDataGrid.CaptionSummaryRow>   
   
  
   
Regards,   
Sivaraman    


Loader.
Up arrow icon