Skip to main content

How to: Calculate Multiple Custom Totals with Custom Summary Type

  • 9 minutes to read

The following example demonstrates how to calculate and display multiple Custom Totals for a field.

In this example, two Custom Totals are implemented for the Category Name field. The first one displays a median calculated against summary values, while the second one displays the first and third quartiles.

To accomplish this task, we create two PivotGridCustomTotal objects and set their summary type to PivotSummaryType.Custom. We also assign the Custom Totals’ names to PivotGridCustomTotalBase.Tag properties to be able to distinguish between the Custom Totals when we calculate their values. Finally, we add the created objects to the Category Name field’s PivotGridField.CustomTotals collection and enable the Custom Totals to be displayed for this field by setting the PivotGridFieldBase.TotalsVisibility property to PivotTotalsVisibility.CustomTotals.

Custom Total values are actually calculated in the ASPxPivotGrid.CustomCellValue event. First, the event handler prepares a list of summary values against which a Custom Total will be calculated. For this purpose, it creates a summary datasource and copies the summary values to an array. After that, the array is sorted and passed to an appropriate method that calculates a median or quartile value against the provided array. Finally, the resulting value is assigned to the event parameter’s PivotCellValueEventArgs.Value property.

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs"
    Inherits="ASPxPivotGrid_MultipleCustomTotals._Default" %>
<%@ Register Assembly="DevExpress.Web.ASPxPivotGrid.v14.2, Version=,
    Culture=neutral, PublicKeyToken=b88d1754d700e49a"
    TagPrefix="dx" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"

<html xmlns="" >
<head runat="server">
    <title>Untitled Page</title>
    <form id="form1" runat="server">
        <dx:ASPxPivotGrid ID="ASPxPivotGrid1" runat="server" 
            oncustomcellvalue="ASPxPivotGrid1_CustomCellValue" onload="ASPxPivotGrid1_Load" 
            OptionsPager-RowsPerPage="20" Theme="Metropolis"> 
                <dx:PivotGridField ID="fieldProductName" Area="RowArea" AreaIndex="1"
                Caption="Product Name" FieldName="ProductName" GroupIndex="1" 
                <dx:PivotGridField ID="fieldShippedYear" Area="ColumnArea" AreaIndex="0" Caption="Year"
                    FieldName="ShippedDate" GroupInterval="DateYear" GroupIndex="0" 
                    InnerGroupIndex="0" UnboundFieldName="fieldShippedYear">
                <dx:PivotGridField ID="fieldProductSales" Area="DataArea" AreaIndex="0" Caption="Sales"
                <dx:PivotGridField ID="fieldCategoryName" Area="RowArea" AreaIndex="0"
                Caption="Category Name" FieldName="CategoryName" GroupIndex="1" 
                <dx:PivotGridField ID="fieldShippedQuarter" Area="ColumnArea" AreaIndex="1"
                Caption="Quarter" FieldName="ShippedDate" 
                ValueFormat-FormatString="Quarter {0}"
                ValueFormat-FormatType="Custom" GroupIndex="0" InnerGroupIndex="1" 
                <dx:PivotGridWebGroup Caption="Date" ShowNewValues="True" />
                <dx:PivotGridWebGroup Caption="Category-Product" ShowNewValues="True" />
        <asp:AccessDataSource ID="AccessDataSource1" runat="server" DataFile="~/nwind.mdb"
            SelectCommand="SELECT [CategoryName], [ProductName], [ProductSales],
            [ShippedDate] FROM [ProductReports]">