Skip to main content
A newer version of this page is available. .

Custom Summaries

  • 4 minutes to read

Custom summaries allow you to manually calculate summaries using custom algorithms. This can be useful in the following instances:

  • calculating a custom summary function;
  • using multiple data fields in a summary calculation;
  • calculating 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. 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 handling the ASPxPivotGrid.CustomSummary event, use the PivotGridCustomSummaryEventArgsBase<T>.CreateDrillDownDataSource method to obtain a subset of records in a datasource which correspond to the currently processed summary cell.

Note

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

Example: How to Calculate a Custom Summary

The following example shows how to calculate a custom summary.Assume that the ASPxPivotGrid control is bound to an "Invoices" table, which contains invoices information (product name, extended price, salesperson, etc). A field that displays the ratio of units cost over $50 is to be added.In this example, a custom summary is calculated against the "Unit Price" field. Its PivotGridCustomTotalBase.SummaryType property is set to PivotSummaryType.Custom and the caption to "Percentage of units cost over $50". The PivotGridControl.CustomSummary event is handled to only count those records whose total sum exceeds $50. The ratio of these records to all the records is a custom summary value and, therefore, is assigned to the PivotGridCustomSummaryEventArgsBase.CustomValue parameter.

using System.Web.UI;
using DevExpress.Web.ASPxPivotGrid;
using DevExpress.XtraPivotGrid;

namespace ASPxPivotGrid_CustomSummary {
    public partial class _Default : Page {
        static int minSum = 500;
        protected void ASPxPivotGrid1_CustomSummary(object sender, 
                                                    PivotGridCustomSummaryEventArgs e) {
            if (e.DataField != fieldExtendedPrice) return;

            // A variable which counts the number of orders whose sum exceeds $500.
            int order500Count = 0;

            // Get the record set corresponding to the current cell.
            PivotDrillDownDataSource ds = e.CreateDrillDownDataSource();

            // Iterate through the records and count the orders.
            for (int i = 0; i < ds.RowCount; i++) {
                PivotDrillDownDataRow row = ds[i];

                // Get the order's total sum.
                decimal orderSum = (decimal)row[fieldExtendedPrice];
                if (orderSum >= minSum) order500Count++;
            }

            // Calculate the percentage.
            if (ds.RowCount > 0) {
                e.CustomValue = (decimal)order500Count / ds.RowCount;
            }
        }
    }
}