How To…Detect the Last Group Detail

Question

“How do I detect the last detail printed in a group?”

Solution

Create a separate dataset that counts the values present for each group based on the grouping field.  You can then use this information to determine when the detail band has reached its final record for that group and you can make alterations to that band’s components as needed.

Download: LastDetailInGroup.zip

Sample Delphi code:

procedure TForm1.ppDetailBand1BeforeGenerate(Sender: TObject);
begin
  FDetailCount := FDetailCount + 1;

end;

procedure TForm1.ppDetailBand1BeforePrint(Sender: TObject);
var
  liDetailCount: Integer;
begin

  liDetailCount := daBDEQueryDataView2.DataPipelines[0]['COUNT(*)'];

  //Hide the line if it is the last detail in the group.
  ppLine1.Visible := liDetailCount <> FDetailCount + 1;

end;

procedure TForm1.ppGroup1AfterGroupBreak(Sender: TObject);
begin
  FDetailCount := 0;
  //After each group move to the next record in the group detail count dataset.
  daBDEQueryDataView2.DataPipelines[0].Next;

end;