0% found this document useful (0 votes)
490 views

Using FastReport - Net To Create Reports From Code

The document describes how to create a report from code using FastReport.NET by: 1. Registering a data source from an XML database and adding it to the report 2. Creating report elements like the page, header, footer, and data band 3. Adding text objects to each element and binding them to data fields 4. Setting properties of the text objects like font, color, borders, and positioning 5. Adding the elements and objects to the report to generate it from code

Uploaded by

victor crespo
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
490 views

Using FastReport - Net To Create Reports From Code

The document describes how to create a report from code using FastReport.NET by: 1. Registering a data source from an XML database and adding it to the report 2. Creating report elements like the page, header, footer, and data band 3. Adding text objects to each element and binding them to data fields 4. Setting properties of the text objects like font, color, borders, and positioning 5. Adding the elements and objects to the report to generate it from code

Uploaded by

victor crespo
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Using FastReport.

NET to Create Reports from Code


Usually, when programming using the FastReport.NET library, it is necessary to load the report template from a file,
show the user in the preview window and enable the user to change the report template or print or export the
report.

Sometimes it is necessary to create a report template from code. This article examines an example of
creating a report from C# code.

The following lists the report requirements:

1. Single page

2. Report header

3. Page header

4. Page heading, containing 6 columns

5. Table with data, received from a demonstration database (nwind.xml)

6. Page footer

7. Report footer.

To create a report, create the data source and register it:

report = new Report();


dataset = new DataSet();
dataset.ReadXml(@"..\..\nwind.xml");
report.RegisterData(dataset);
report.GetDataSource("Products").Enabled = true;

The products table from the demonstration database is used as the data source.

Create the report page using the following code fragment:


ReportPage page = new ReportPage();
page.CreateUniqueName();
page.TopMargin = 10.0f;
page.LeftMargin = 10.0f;
page.RightMargin = 10.0f;
page.BottomMargin = 10.0f;
report.Pages.Add(page);

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.

The created page is added to the report.

Use the following code to create the report header using the ReportTitle page property:

page.ReportTitle = new ReportTitleBand();


page.ReportTitle.CreateUniqueName();
page.ReportTitle.Height = 4.0f * Units.Centimeters;

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 following code creates the page header text object:

TextObject headerText = new TextObject();


headerText.CreateUniqueName();
headerText.Bounds = new RectangleF(0.0f, 0.5f * Units.Centimeters,
19.0f * Units.Centimeters, 1.0f * Units.Centimeters);
headerText.HorzAlign = HorzAlign.Center;
headerText.VertAlign = VertAlign.Center;
headerText.Font = new Font("Book Antique", 16.0f,
FontStyle.Bold | FontStyle.Italic);
headerText.TextColor = Color.Teal;
2
headerText.FillColor = Color.YellowGreen;
headerText.Border.Lines = BorderLines.All;
headerText.Border.TopLine.Color = Color.Indigo;
headerText.Border.LeftLine.Color = Color.Gold;
headerText.Border.RightLine.Color = Color.Gold;
headerText.Border.BottomLine.Color = Color.Indigo;
headerText.Border.TopLine.Width = 3.0f;
headerText.Border.LeftLine.Width = 2.0f;
headerText.Border.RightLine.Width = 2.0f;
headerText.Border.BottomLine.Width = 3.0f;
headerText.Text = TableName + " Table";
page.PageHeader.Objects.Add(headerText);

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 following code creates the databand:


DataBand band = new DataBand();
page.Bands.Add(band);
band.CreateUniqueName();
band.DataSource = report.GetDataSource("Products");
band.Height = 0.5f * Units.Centimeters;

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:

TextObject bandText = new TextObject();


bandText.CreateUniqueName();
bandText.HorzAlign = HorzAlign.Center;
bandText.Bounds = new RectangleF(0.0f * Units.Centimeters, 0.0f,
1.0f * Units.Centimeters, 0.5f * Units.Centimeters);
bandText.Border.Lines = BorderLines.All;
bandText.Text = "[Products.ProductID]";
band.AddChild(bandText);

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:

page.PageFooter = new PageFooterBand();


page.PageFooter.CreateUniqueName();
page.PageFooter.Height = 0.5f * Units.Centimeters;
TextObject footerText = new TextObject();
footerText.CreateUniqueName();
footerText.HorzAlign = HorzAlign.Right;
footerText.VertAlign = VertAlign.Center;
footerText.Bounds = new RectangleF(0.0f, 0.0f,
19.0f * Units.Centimeters, 0.5f * Units.Centimeters);
footerText.TextColor = Color.Teal;
footerText.FillColor = Color.YellowGreen;
footerText.Border.Lines = BorderLines.All;
footerText.Border.TopLine.Color = Color.Indigo;
footerText.Border.LeftLine.Color = Color.Gold;
3
footerText.Border.RightLine.Color = Color.Gold;
footerText.Border.BottomLine.Color = Color.Indigo;
footerText.Border.TopLine.Width = 3.0f;
footerText.Border.LeftLine.Width = 2.0f;
footerText.Border.RightLine.Width = 2.0f;
footerText.Border.BottomLine.Width = 3.0f;
footerText.Text = "Page [Page]";
page.PageFooter.Objects.Add(footerText);

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 final code fragment creates the report footer:

page.ReportSummary = new ReportSummaryBand();


page.ReportSummary.CreateUniqueName();
page.ReportSummary.Height = 4.0f * Units.Centimeters;
footerText = new TextObject();
footerText.CreateUniqueName();
footerText.Left = 1.0f * Units.Centimeters;
footerText.Top = 1.0f * Units.Centimeters;
footerText.Width = 17.0f * Units.Centimeters;
footerText.Height = 2.0f * Units.Centimeters;
footerText.HorzAlign = HorzAlign.Center;
footerText.VertAlign = VertAlign.Center;
footerText.Font = new Font("Chiller", 32.0f, FontStyle.Bold);
footerText.TextColor = Color.DarkGreen;
footerText.FillColor = Color.DarkOrange;
footerText.Border.Color = Color.DarkOrchid;
footerText.Border.Lines = BorderLines.All;
footerText.Border.Width = 4.0f;
footerText.Text = "Total Pages [TotalPages#]";
page.ReportSummary.Objects.Add(footerText);

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.

Figure 3 shows the dialogue window.

Fig 3

The function for this window is as follows:

1. The window appears before the report preview window

2. Click OK to see a preview

3. Click Cancel if the preview window is not required.

The following code creates the window:

report.Load("report.frx");

DialogPage dialog = new DialogPage();

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.

The following code creates the OK button:

ButtonControl buttonOK = new ButtonControl();


buttonOK.Name = "ButtonOK";
buttonOK.Location = new Point(25, 25);
buttonOK.Size = new Size(75, 25);
buttonOK.Text = "OK";
buttonOK.DialogResult = DialogResult.OK;
dialog.Controls.Add(buttonOK);

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 cancel button is created in the same way:

ButtonControl buttonCancel = new ButtonControl();


buttonCancel.Name = "ButtonCancel";
buttonCancel.Location = new Point(25, 55);
buttonCancel.Size = new Size(75, 25);
buttonCancel.Text = "Cancel";
buttonCancel.DialogResult = DialogResult.Cancel;
dialog.Controls.Add(buttonCancel);

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.

Adding a connection to a report


Add a new connection to the report by using an empty report from the previous report. Add the
connection to the demonstartion database(demo.mdb) that is contained in FastReport Studio.

Use the following code:


report.Load("report.frx");

MsAccessDataConnection connection = new MsAccessDataConnection();


connection.Name = "Connection1";
connection.DataSource = "demo.mdb";
report.Dictionary.Connections.Add(connection);
connection.CreateAllTables();
report.GetDataSource("customer").Enabled = true;

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.

The MsAccessDataConnection class is located in FastReport.Data.

You might also like