Douglas Lise
Douglas Lise

Reputation: 1486

How to show a running total in a DevExpress PivotGrid?

I need to show totals like the total plus the last total in a DevExpress PivotGrid.
Example:
Taking this values: March: 25
April: 10
May: 30

Resulting in this:

March   April  May
25      35     65

How do I configure my PivotGrid to this?

Thanks in advance.

Upvotes: 0

Views: 4976

Answers (2)

Douglas Lise
Douglas Lise

Reputation: 1486

You need to add this code in the field's OnCalculateCustomSummary event:

procedure TMyForm.cxDBPivotGrid1Field2CalculateCustomSummary(Sender: TcxPivotGridField; ASummary: TcxPivotGridCrossCellSummary);
var
  AColumn: TcxPivotGridGroupItem;
  APrevCrossCell: TcxPivotGridCrossCell;
  APrevCrossCellSummary: TcxPivotGridCrossCellSummary;
begin
  inherited;
  AColumn := ASummary.Owner.Column;
  if (AColumn.Level = -1) then
    // column grand totals
    ASummary.Custom := ASummary.Sum
  else begin
    // all cells, except for column grand totals
    // getting a custom summary calculated for the previous grouping value
    if AColumn.PrevSibling <> nil then begin
      APrevCrossCell        := AColumn.PrevSibling.GetCellByCrossItem(ASummary.Owner.Row);
      APrevCrossCellSummary := APrevCrossCell.SummaryCells[Sender.SummaryIndex];
      ASummary.Custom       := VarToDouble(APrevCrossCellSummary.Custom) + VarToDouble(ASummary.Sum);
    end
    else
      ASummary.Custom       := ASummary.Sum;
  end;
end;

And this in the field's OnGetDisplayText event:

procedure TForm1.cxDBPivotGrid1Field2GetDisplayText(Sender: TcxPivotGridField; ACell: TcxPivotGridDataCellViewInfo; var AText: string);
begin
  inherited;
  AText := VarToStr(ACell.CellSummary.Custom)
end;

Extracted from: http://www.devexpress.com/Support/Center/p/Q90021.aspx

Upvotes: 1

DevExpress Team
DevExpress Team

Reputation: 11376

You should set the Field's RunningTotal property to true.

Upvotes: 0

Related Questions