Skip to main content

Creating Custom Summaries

  • 5 minutes to read

The ExpressQuantumGrid provides two events that allow a user to control summary calculations. These events are

Handling these events allows you to change the default summary calculations or implement your own functions for summary calculations.

The OnSummary event occurs when calculating a summary value. The event parameters are used to identify the summary item to be calculated, the currently calculated value (for instance, for Sum summary type), the number of records involved in the summary calculation and other summary parameters.

The following code alters a Record count summary to count only those fields with values greater than 300000:

procedure TSummaryMultiDemoMainForm.tvOrdersDataControllerSummaryDefaultGroupSummaryItemsSummary(
  ASender: TcxDataSummaryItems; Arguments: TcxSummaryEventArguments;
  var OutArguments: TcxSummaryEventOutArguments);
var
  AValue: Variant;
  AItem: TcxGridTableSummaryItem;
begin
//the summary item whose value is calculated is identified by the
//TcxSummaryEventArguments.SummaryItem property
  AItem := TcxGridTableSummaryItem(Arguments.SummaryItem);
//if the required item is calculated against the ProductID column, its type
//is Count and it is displayed within the group row...
  if (AItem.Column = tvOrdersProductID) and
    (AItem.Kind = skCount) and (AItem.Position = spGroup) then
  begin
//...then the AValue variable is assigned the value of PaymentAmount field of
//a record, identified by the TcxSummaryEventArguments.RecordIndex property
    AValue := tvOrders.DataController.Values[
      Arguments.RecordIndex, tvOrdersPaymentAmount.Index];
    if not VarIsNull(AValue) then
//if the obtained value is less than or equal to 300000, then...
      if VarAsType(AValue, varInteger) <= 300000 then
//...the TcxSummaryOutArguments.CountValue property, representing the number
//of records involved into summary calculations, is decreased by 1
       Dec(OutArguments.CountValue);
  end;
end;

The result of executing the above code is demonstrated below. The calculated summary and the corresponding PaymentAmount column values are emphasized:

The OnAfterSummary event occurs after all summary values have been calculated. Handling it allows you to work with any summary value, even those which are not currently visible. This event accepts one argument – an instance of the TcxDataSummary class, representing all the summaries calculated within a View.

The following code iterates through the summaries of all groups in a level calculating the maximum value and displays it as the root group level summary.

procedure TSummaryGroupDemoMainForm.tvOrdersDataControllerSummaryAfterSummary(
  ASender: TcxDataSummary);
var
  AItem: TcxGridTableSummaryItem;
  AGroups: TcxDataControllerGroups;
  AValue, AMaxValue: Variant;
  I: Integer;
begin
  //The first footer summary item, displayed within the PaymentAmount column footer
  //is formatted to display the maximum value of the first group summary item displayed
  //at the root group level
  AItem := TcxGridTableSummaryItem(ASender.FooterSummaryItems[0]);
  AItem.Format := 'The maximum number of sold cars is: 0';
  AGroups := ASender.DataController.Groups;
  //the AMaxValue variable contains the value of the first required group summary item
  AMaxValue := ASender.GroupSummaryValues[AGroups.ChildDataGroupIndex[-1, 0], 0];
  //for all group rows...
  for I := 1 to AGroups.ChildCount[-1] - 1 do
  begin
  //the AValue variable contains the value of the required group summary item
      AValue := ASender.GroupSummaryValues[AGroups.ChildDataGroupIndex[-1, I], 0];
  //if the obtained value is greater than the current maximum, then the current maximum value is replaced with the obtained value
      if AValue > AMaxValue then AMaxValue := AValue;
  end;
  //the value of the first footer summary item is set to maximum value of the first group summary item at the roow group level
  ASender.FooterSummaryValues[0] := AMaxValue;
end;

The result of executing the above code is demonstrated below. The calculated value of the footer summary and the corresponding group summary value are emphasized:

See Also