How To…Create Report Fields in Code

Question

“How do I dynamically create a report in code based on the fields in a datapipeline?”

Solution

Yes, this is a matter of simply looping through all the fields in the datapipeline(s) and dynamically creating labels/dbTexts for each field in code.

Download: DynamicFields.zip

Sample Delphi code:

procedure TForm1.CreateText;
var
  liIndex: Integer;
  liNumFields: Integer;
  lLabel: TppLabel;
  lDBText: TppDBText;
  lField: TppField;
begin

  liNumFields := ppDBPipeline1.FieldCount;

  CreateLabels(liNumFields, FLabelList);
  CreateDBTexts(liNumFields, FDBTextList);

  for liIndex := 0 to liNumFields - 1 do
    begin
      lLabel := TppLabel(FLabelList[liIndex]);
      lDBText := TppDBText(FDBTextList[liIndex]);
      lField := ppDBPipeline1.Fields[liIndex];

      lLabel.Band := ppHeaderBand1;
      lLabel.Left := liIndex;
      lLabel.Top := 0.25;
      lLabel.Caption := lField.FieldAlias;
      lLabel.Font.Size := 12;
      lLabel.Font.Color := clBlue;

      lDBText.Band := ppDetailBand1;
      lDBText.DataPipeline := ppDBPipeline1;
      lDBText.Left := liIndex;
      lDBText.Top := 0.25;
      lDBText.DataField := lField.FieldName;

    end;


end;

procedure TForm1.CreateLabels(aFieldCount: Integer; aList: TList);
var
  lLabel: TppLabel;
  liIndex: Integer;
begin

  for liIndex := 0 to aFieldCount - 1 do
    begin
      lLabel := TppLabel.Create(Self);
      aList.Add(lLabel);
    end;

end;

procedure TForm1.CreateDBTexts(aFieldCount: Integer; aList: TList);
var
  lDBText: TppDBText;
  liIndex: Integer;
begin

  for liIndex := 0 to aFieldCount - 1 do
    begin
      lDBText := TppDBText.Create(Self);
      aList.Add(lDBText);
    end;

end;