Custom Summaries

  • 4 minutes to read

Custom summaries allow you to calculate summaries using custom algorithms. You can:

  • calculate a custom summary function;
  • use multiple data fields in a summary calculation;
  • calculate a summary for individual records (for instance, for the records which match specific criteria).

To enable a custom summary calculation, set the data field's PivotGridFieldBase.SummaryType property to PivotSummaryType.Custom. Then handle the ASPxPivotGrid.CustomSummary event to implement a custom summary calculation algorithm. This event is fired for each summary cell that corresponds to this data field.

When you handle the ASPxPivotGrid.CustomSummary event, use the PivotGridCustomSummaryEventArgsBase<T>.CreateDrillDownDataSource method to obtain a subset of records in a datasource that correspond to the currently processed summary cell.

NOTE

Custom summaries calculated in the ASPxPivotGrid.CustomSummary event are not supported in server mode.

Example: How to Calculate a Custom Summary

This example demonstrates different approaches to calculate a custom summary.

The task is to calculate the percentage of units that cost over $50 and show that value in the total columns.

The following approaches are shown:

  • The CustomSummary event. A custom summary is calculated for the "Unit Price" field. The field's SummaryType property is set to PivotSummaryType.Custom. The ASPxPivotGrid.CustomSummary event is handled to count the records whose total sum exceeds $50. The ratio of these records to all the records is assigned to the e.CustomValue parameter.

  • An unbound expression. A field is an unbound field with an expression that calculates the ratio.

  • Data Binding API. In Optimized mode create a field bound to the ExpressionDataBinding object with the following expression:

    ToDecimal(Sum(iif([Extended_Price]>=500,1,0)))/Count()
    

View Example: How to Implement Custom Summary

<dx:ASPxPivotGrid ID="ASPxPivotGrid1" runat="server" DataSourceID="AccessDataSource1" Theme="Office365">
    <OptionsData DataProcessingEngine="Optimized" />
    <Fields>
        <dx:PivotGridField ID="fieldProductName" Area="RowArea" AreaIndex="0"
            Caption="Product Name" FieldName="ProductName">
        </dx:PivotGridField>
        <dx:PivotGridField ID="fieldExtendedPriceNew" Area="DataArea" AreaIndex="0"
            Caption="Percentage of Orders over $500">
            <DataBindingSerializable>
                <dx:ExpressionDataBinding Expression="ToDecimal(Sum(iif([Extended_Price]>=500,1,0)))/Count()" />
            </DataBindingSerializable>
            <CellFormat FormatString="p" FormatType="Numeric"></CellFormat>
        </dx:PivotGridField>
    </Fields>
</dx:ASPxPivotGrid>