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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
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; |