XRPivotGrid.CustomSummary Event
Enables summary values to be calculated manually.
Namespace: DevExpress.XtraReports.UI
Assembly: DevExpress.XtraReports.v24.2.dll
NuGet Package: DevExpress.Reporting.Core
Declaration
Event Data
The CustomSummary event's data class is DevExpress.XtraReports.UI.PivotGrid.PivotGridCustomSummaryEventArgs.
Remarks
The XRPivotGrid calculates summaries against data fields. A field’s PivotGridFieldBase.SummaryType property specifies the type of summary function. The control automatically calculates all the predefined summary functions (see the PivotSummaryType topic for a list of the available functions) and it allows custom summaries to be calculated manually via the CustomSummary event.
To calculate a custom summary for a specific data field set its PivotGridFieldBase.SummaryType property to PivotSummaryType.Custom. In this case, the CustomSummary event will fire for each cell that corresponds to this data field.
For instance, this event enables a custom summary to be calculated against multiple fields, particular records, etc. Use the PivotGridCustomSummaryEventArgsBase`1.CreateDrillDownDataSource method to get a list of the records that correspond to the current cell. This list can then be traversed to calculate a summary in a custom manner. The custom summary value should be assigned to the PivotGridCustomSummaryEventArgsBase`1.CustomValue parameter.
For each cell the XRPivotGrid calculates all the predefined summaries (AVERAGE, MIN, MAX, SUM, etc). The calculated values can be accessed via the PivotGridCustomSummaryEventArgsBase`1.SummaryValue property. You can use them in custom summary calculations.
Example
The following example shows how to calculate a custom summary.
Assume that the Pivot Grid Control is bound to a “SalesPerson” view. A field which displays the ratio of orders over $500 is to be added.
In this example, the custom summary is calculated against the “Extended Price” field. It’s PivotGridCustomTotalBase.SummaryType property is set to PivotSummaryType.Custom and the caption to “Percentage of Orders over $500”. The PivotGridControl.CustomSummary event is only handled to count those records whose total sum exceeds minSum. The ratio of these records to all the records is a custom summary value and therefore is assigned to the PivotGridCustomSummaryEventArgsBase`1.CustomValue parameter.
The following image shows the result of the custom summary calculation:
using DevExpress.XtraPivotGrid;
fieldExtendedPrice.Caption = "Percentage of Orders over $500";
// Enable a custom summary calculation for the Extended Price field.
fieldExtendedPrice.SummaryType = DevExpress.Data.PivotGrid.PivotSummaryType.Custom;
// Specify the settings used to format values.
fieldExtendedPrice.CellFormat.FormatType = DevExpress.Utils.FormatType.Numeric;
fieldExtendedPrice.CellFormat.FormatString = "p";
int minSum = 500;
private void pivotGridControl1_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;
}
}