How To…Show a Total in the Last Detail

Question

“How can I show the sum of the previous details in the last detail band of a group, rather than using the group footer or footer band?”

Solution

This can be done by making the calculation in a TppVariable, then manually adding a DrawCommand to the page aligned with the last detail band of each group.  Start by placing an invisibile TppVariable on the report to make the calcuation.  Implement its OnCalc event to calculate the sum of the group.  Next call a routine in the FooterBand.BeforePrint event that creates a DrawText command and assign the value of the variable to its value.  Finally position the DrawText command to the point on the page where the last detail band was printed.

Download:  GroupSumInLastDetail.zip

Sample Delphi code:

procedure TForm1.DrawText;
var
  lDrawText: TppDrawText;
  liTop: Integer;
  liWidth: Integer;
  lBitmap: TBitmap;
begin

  lBitmap := TBitmap.Create;

  if (FCurrentPosition <> -1) then
    begin
      liTop := FCurrentPosition;

      lDrawText := TppDrawText.Create(nil);
      lDrawText.Text := ppVariable1.Text;
      lDrawText.Font.Size := 10;
      lDrawText.Font.Name := 'Arial';
      lDrawText.Font.Color := clRed;
      lDrawText.Color := clWhite;
      lDrawText.Page := ppReport1.Engine.Page;
      lDrawText.Height := 3800;
      lDrawText.Left := ppReport1.PrinterSetup.PageDef.mmMarginLeft + (ppReport1.PrinterSetup.PageDef.mmPrintableWidth) div 2;
      lDrawText.Top := liTop - 3800;

      liWidth := lBitmap.Canvas.TextWidth(lDrawText.Text);
      liWidth := ppToMMThousandths(liWidth, utScreenPixels, pprtHorizontal, ppReport1.Printer);

      lDrawText.Width := liWidth;
    end;

  lBitmap.Free;

end;

procedure TForm1.ppReport1StartPage(Sender: TObject);
begin
  FCurrentPosition := -1;
end;

procedure TForm1.ppGroupHeaderBand1BeforePrint(Sender: TObject);
begin
  FCurrentPosition := -1;
end;

procedure TForm1.ppDetailBand1AfterPrint(Sender: TObject);
begin
  FCurrentPosition := ppReport1.Engine.PrintPosRect.Top;
end;

procedure TForm1.ppGroupFooterBand1BeforePrint(Sender: TObject);
begin
  DrawText;
end;

procedure TForm1.ppVariable1Calc(Sender: TObject; var Value: Variant);
begin
  Value := Value + ppReport1.DataPipeline['Amountpaid'];
end;