How the report generator FastReport .Net uses code to create reports

FastReport .Net is a full-featured report solution for Windows Forms, ASP.NET, MVC and .NET Core. It can be used in Microsoft Visual Studio 2005-2019. Support .Net Framework 2.0-4.x, .NET Core 3.0 and above.

In the new version of FastReport .NET 2021.1, we have implemented support for .NET 5. Added a new barcode-Deutsce Post Leitcode. The algorithm for converting RTF into report objects has been significantly improved. And also added a new function for converting numbers.

Click to download the latest version of FastReport.NET v2021.1 now

Use code to create reports

Let us consider how to create a report in code. We will create the following report.
Insert picture description here

 Report report = new Report();
// register the "Products" table
report.RegisterData(dataSet1.Tables["Products"], "Products");
// enable it to use in a report
report.GetDataSource("Products").Enabled = true;
// create A4 page with all margins set to 1cm
ReportPage page1 = new ReportPage();
page1.Name = "Page1";
// create ReportTitle band
page1.ReportTitle = new ReportTitleBand();
page1.ReportTitle.Name = "ReportTitle1";
// set its height to 1.5cm
page1.ReportTitle.Height = Units.Centimeters * 1.5f;
// create group header
GroupHeaderBand group1 = new GroupHeaderBand();
group1.Name = "GroupHeader1";
group1.Height = Units.Centimeters * 1;
// set group condition
group1.Condition = "[Products.ProductName].Substring(0, 1)";
// add group to the page.Bands collection
// create group footer
group1.GroupFooter = new GroupFooterBand();
group1.GroupFooter.Name = "GroupFooter1";
group1.GroupFooter.Height = Units.Centimeters * 1;
// create DataBand
DataBand data1 = new DataBand();
data1.Name = "Data1";
data1.Height = Units.Centimeters * 0.5f;
// set data source
data1.DataSource = report.GetDataSource("Products");
// connect databand to a group
group1.Data = data1;
Working with Windows.Forms 23
// create "Text" objects
// report title
TextObject text1 = new TextObject();
text1.Name = "Text1";
// set bounds
text1.Bounds = new RectangleF(0, 0,
Units.Centimeters * 19, Units.Centimeters * 1);
// set text
text1.Text = "PRODUCTS";
// set appearance
text1.HorzAlign = HorzAlign.Center;
text1.Font = new Font("Tahoma", 14, FontStyle.Bold);
// add it to ReportTitle
// group
TextObject text2 = new TextObject();
text2.Name = "Text2";
text2.Bounds = new RectangleF(0, 0,
Units.Centimeters * 2, Units.Centimeters * 1);
text2.Text = "[[Products.ProductName].Substring(0, 1)]";
text2.Font = new Font("Tahoma", 10, FontStyle.Bold);
// add it to GroupHeader
// data band
TextObject text3 = new TextObject();
text3.Name = "Text3";
text3.Bounds = new RectangleF(0, 0,
Units.Centimeters * 10, Units.Centimeters * 0.5f);
text3.Text = "[Products.ProductName]";
text3.Font = new Font("Tahoma", 8);
// add it to DataBand
// group footer
TextObject text4 = new TextObject();
text4.Name = "Text4";
text4.Bounds = new RectangleF(0, 0,
Units.Centimeters * 10, Units.Centimeters * 0.5f);
text4.Text = "Count: [CountOfProducts]";
text4.Font = new Font("Tahoma", 8, FontStyle.Bold);
// add it to GroupFooter
// add a total
Total groupTotal = new Total();
groupTotal.Name = "CountOfProducts";
groupTotal.TotalType = TotalType.Count;
groupTotal.Evaluator = data1;
groupTotal.PrintOn = group1.Footer;
// add it to report totals
// run the report

The prepared report is as follows:
Insert picture description here

Use your own preview window

Using the EnvironmentSettings component (see the "Configuring the FastReport.Net Environment" section), you can adjust the standard preview window. Related properties are contained in the EnvironmentSettings.PreviewSettings property.

If you do not want to use the standard preview window for some reason, you can create your own preview window. To do this, use the PreviewControl control that can be added to your form. To display a report in this control, connect it to the Report object through the following code.

report1.Preview = previewControl1;

To prepare a report and display it in PreviewControl, use the Show method of the Report object.


Or the following code:

 if (report1.Prepare())

In these examples, your_form is the form that contains PreviewControl.
Using the methods of the PreviewControl component, you can handle it from your code. You can even use the ToolbarVisible, StatusbarVisible properties to turn off the standard toolbar and/or status bar. This is demonstrated in the Demos/\C#/CustomPreview sample project.

Filter tables in the data wizard

The data wizard can be called from the "Data|Add Data Source..." menu. Here you can set up a connection and select one or more data tables. By default, the wizard displays all available tables in the selected connection. If you want to filter unnecessary tables, please use the "Config.DesignerSettings.FilterConnectionT" menu. DesignerSettings.FilterConnectionTables event. The following example shows how to delete the "Table 1" table from the table list.

using FastReport.Utils;
Config.DesignerSettings.FilterConnectionTables += FilterConnectionTables;
private void FilterConnectionTables(
object sender, FilterConnectionTablesEventArgs e)
if (e.TableName == "Table 1")
e.Skip = true;

Guess you like