Using FastReport - Net To Create Reports From Code
Using FastReport - Net To Create Reports From Code
Sometimes it is necessary to create a report template from code. This article examines an example of
creating a report from C# code.
1. Single page
2. Report header
3. Page header
6. Page footer
7. Report footer.
The products table from the demonstration database is used as the data source.
A unique name for the page is created in the second row. The method CreateUniqueName () is accessible in any
FastReport.NET component. The report page has been named “Page1”. It can be renamed using the
CreateUniqueName method and the Name property:
page.Name = "FirstPage";
1
The page fields are detailed in the following four code rows (using the properties TopMargin, LeftMargin,
RightMargin and BottomMargin_. The page is created with fields of the same size by default but the properties are
not obligatory.
Use the following code to create the report header using the ReportTitle page property:
The header height in the third row is 4 cm. The property stores height in pixels and needs to be renamed to use
Units.Centimeters (the number of pixels in one centimeter). Connect to FastReport.Utils by creating the text
object in the header:
TextObject titleText = new TextObject();
titleText.CreateUniqueName();
titleText.Left = 1.0f * Units.Centimeters;
titleText.Top = 1.0f * Units.Centimeters;
titleText.Width = 17.0f * Units.Centimeters;
titleText.Height = 2.0f * Units.Centimeters;
titleText.HorzAlign = HorzAlign.Center;
titleText.VertAlign = VertAlign.Center;
titleText.Font = new Font("Chiller", 32.0f, FontStyle.Bold);
titleText.TextColor = Color.DarkGreen;
titleText.FillColor = Color.DarkOrange;
titleText.Border.Color = Color.DarkOrchid;
titleText.Border.Lines = BorderLines.All;
titleText.Border.Width = 4.0f;
titleText.Text = "Report from code demo";
page.ReportTitle.Objects.Add(titleText);
The position of the object relative to the report header is indicated via the Left and Top properties; the size is
indicated using the width and height properties. The following two rows set the horizontal and vertical alignment of
the text in the object. The font of the text can be chosen using the Font property. The name, size and style of the
font is shown.The following three rows show the text colour, filled text object colour and the colour of the border.
The last two rows show which border will be visible and the width in pixels. The text is displayed in the last row and
the last row of the text object is added to the report header.
The page header is given in the same way. Six text objects are created apart from the page header text and are table
headings. The report header and page header are similar in many respects.The main difference is that the report
header is printed on the first page of the report and the page header on every page.
The Bounds property enables the creation of a rectangle that describes the object border providing the position,
width and height of the object in one row. A row displays the colour and border width of the text . It is possible to
set the colour of each of the borders separately.
The fourth row shows the data source for the band. Six text objects are created in the band that will be connected
to the table fields. The following code creates one of the objects; the remainder will be created in the same manner:
In the last two rows, the first creates a connection to the ProductID field in the Products table and the second adds
the text to the band. Providing the text object with a parent can be performed using the following code:
bandText.Parent = band;
The following code fragment creates the pagefooter with the text object in it:
The penultimate row shows the Page footer text ("Page [Page]"). Apart from the Page text, the Page – system
variable is used for numbering pages and storing the current report page.
The penultimate row uses the system variable TotalPages# in which the total number of pages in the report is
saved. The main difference between the report footer and page footer is just the same as between the report
header and the page header; the page footer is printed on every page and the report footer on the last page only.
The report template is shown in figures 1 and 2 and the report pages in preview.
4
Fig 1.
5
Fig 2.
6
Adding a Dialogue Window to a Report
It is possible to use FastReport.NET to modify existing reports. The following example adds a dialogue window to a
report. Create a simple report containing empty pages in the designer by clicking menu File New and selecting and
saving a blank report in the drop down menu.
Fig 3
report.Load("report.frx");
dialog.Name = "Dialog1";
dialog.Form.Size = new Size(125, 125);
report.Pages.Add(dialog);
The first code loads the report from the previously created. The report was saved in the application folder
so that the load function does not need to be given the full folder file. The following three rows create the
dialogue window, creating the name and size. The last row adds the form to the report page list.
7
Create a button, name it, position it in the window, and set the size and the text. The penultimate row of
code determines the standard behaviour of the OK button. The final row adds the button to the dialogue
window.
The DialogPage and ButtonControl classes are located within the FastReport.Dialog area. Access to it is via
a connecting word or by showing the name.
Load the report, create the connection, show its name, the path to the database (which is located in the
application folder) and register the connection in the report. The penultimate row loads the whole table and
the last row makes the customer table accessible in the report.