Hi Lajos,
Thanks for your reply - your response got me to think and that got me to find where I made an error from the example but also I now understand the actual FastReport control better.
Below I have posted a copy of the altered code where it not only works - but it reports all the fields properly as well. There are lots of refinements but this actually works!
I couldn't find an example like this anywhere so I hope this helps someone else.
Once again thanks Lajos for pointing me in the right direction.
Best regards,
Haley
The below code assumes that you have an DBGrid that is linked to a SQLDataSource that is linked to an FDQuery. The DBGrid shows results from the FDQuery and so I am using the FDGrid's widths. I don't know yet how to get the field names from frxDBDataSet so I get them directly from FDQuery - not that elegant but also not bad for code written between a gym session and other homework.
procedure TMainForm.mnuPrint_OnExecute(Sender: TObject);
var
FieldCounter, Horizontal : Integer;
frxReport : TfrxReport;
frxDBDataSet : TfrxDBDataSet; // Was TfrxDataSet many moons ago
DataPage: TfrxDataPage;
Page: TfrxReportPage;
Band: TfrxBand;
DataBand: TfrxMasterData;
Memo: TfrxMemoView;
begin
if (FDQuery.RecordCount > 0) then
begin
try
// Create an instance of the Report Control
frxReport:= TfrxReport.Create(Self);
frxReport.Clear;
// Create an instance of the Dataset and link it to the FDQuery
frxDBDataSet:= TfrxDBDataSet.Create(frxReport.Owner); // Was TfrxDataSet
frxDBDataSet.Name:= 'UserData';
frxDBDataSet.UserName:= Name;
frxDBDataSet.DataSet:= FDQuery;
// Link the Report to the Dataset
frxReport.DataSets.Add(frxDBDataSet);
// Add a Datapage
DataPage := TfrxDataPage.Create(frxReport);
// Add a Page
Page := TfrxReportPage.Create(frxReport);
Page.CreateUniqueName;
Page.SetDefaults;
Page.Orientation:= TPrinterOrientation.poPortrait; //.poLandscape;
Page.PaperSize:= DMPAPER_A4;
// Add a report title Band
Band := TfrxReportTitle.Create(Page);
Band.CreateUniqueName;
Band.Top := 0;
Band.Height := 20;
// Add object to report title band
Memo := TfrxMemoView.Create(Band);
Memo.CreateUniqueName;
Memo.Text := 'Report Title';
Memo.Font.Height:= 30;
Memo.Font.Style:= [fsBold];
Memo.Height := 30;
Memo.Align := baWidth; //baWidth;
// Add masterdata band
DataBand := TfrxMasterData.Create(Page);
DataBand.CreateUniqueName;
DataBand.DataSet := frxDBDataSet;
// “Top” should be greater than previously added band’s top + height
DataBand.Top := 100;
DataBand.Height := 20;
Horizontal:= 0;
for FieldCounter := 0 to frxDBDataSet.FieldsCount - 1 do
begin
// add the fields one by one
Memo := TfrxMemoView.Create(DataBand); // Existed
Memo.CreateUniqueName;
// connect the object to dataset
Memo.DataSet := frxDBDataSet;
Memo.DataField := FDQuery.Fields[FieldCounter].FieldName; // This should be from frxDBDataSet
Memo.SetBounds(Horizontal, 0, SQL_DBGrid.Columns[FieldCounter].Width, 20); // Where else do you get widths from
Horizontal:= Horizontal + SQL_DBGrid.Columns[FieldCounter].Width + 10; // Add the widths and a buffer
Memo.HAlign := haLeft; // haRight, bacenter;
end;
// show the report
frxReport.ShowReport;
finally
// Once we have finished free up the controls
frxDBDataSet.Free;
frxReport.Free;
end;
end;
end;
Edited by: Haley Winters on Mar 7, 2016 6:11 PM
Connect with Us