At the new work place I got to deal with the report generator FastReport .NET. Previously, I had to deal with other reporting systems, for example, Crystal Reports and Microsoft Reporting Services. However, FastReport pleasantly surprised me.
It's a really powerful tool with broad functionality. One of my favorite features of FastReport .Net is the ability to create reports directly from the user application code. In this article I want to look at the example of this feature. It comes in handy when you do not need a bunch of files that come with the exe-file. In addition, you can fully control the creation of self-report, changing the appearance of the object of the report, depending on the application logic.
First of all, I will show the difference between building a report from the code of the user application from the classic development of a report template in a special designer.
Usually, the report generator provides a special designer to design a report template. This may be the component IDE, or just external program. Developer places components on the page of the report and specifies their properties. This is similar to designing a form in application Windows Forms application.
In addition to such classic ways to create a report template, FastReport allows you to create a template using the same components but with the help of code in the application. You can also create a report object, add components to it, and configure the data source. With some practice, the creation of a report from the code takes a little longer than in the visual editor. Interestingly, with the result that such a report template can be viewed in the same visual editor (the designer) and save to file.
Let's have a look at the example.
Create a Windows Forms application in the language C # (of course FastReport .NET should be installed at this point).
Place a button on the form, which will launch our report. Looking ahead, I will say that we will not only show the report in preview mode, but also make its exports to PDF. Therefore add CheckBox:
Create a button click event handler. Here is the entire code of the application.
First of all, add a reference to the FastReport.dll (which is in the pack FastReport .Net).
Also, add the library FastReport, FastReport.Utils and FastReport.Data in using.
Create an instance of Report:
Our report will display data from the database, so you need to create a data source:
I took the database from the delivery of FastReport .Net from the folder Reports.
Now you need to register the data source in the report:
To use the table of the registered data source, you need to enable it:
The preparatory work can be considered done. Moving on to creation of the report template. Create the report page:
And add it to the report:
All objects of the report need to be given unique names. You can come up with their own, and assign the property Name, or you can use a function that generates a unique name:
So, the report page is ready for filling. Create a band "Group Header":
Add the created band to the page:
Set the band height:
Grouping condition and sort order:
Now we need to fill the created band with the data. To do this, create a text object with reference to the field from the data source:
Important Parent parameter indicates the band, where will be placed text object:
Set the size and the bounds of the text objects:
And the text:
The other settings relate to the appearance of the text:
Now the most interesting part is the creation of the band "data":
Assign the data band for the group:
Assign the data source for the band "data":
data.DataSource = report.GetDataSource("Products");
data.Height = Units.Centimeters * 0.5f;
Here you can set the filter band with the property Filter. Now fill the band with the text object:
The group footer is made for a specific group instance. It's very convenient because it will not allow to get confused if there are multiple group headers. So, let's create the group footer:
Add a total to the group footer. It will display the count of products in the group:
Set the type calculation, the band for which the calculations are performed, and the band, which will display the results. Since we define the count of items we do not need to specify a particular field for calculations (done using groupTotal.Expression).
We need to add the created total to the totals dictionary of the report. To register it:
Like any expression to be displayed, the result is displayed through the text object:
That's all. The report is ready. Now we can display it, or run in the designer. And you can immediately export it in the desired data format. Let's use a CheckBox, which we added to the form:
If the CheckBox is checked, you will get the dialog box to save the pdf file. Otherwise, the report will be launched in preview mode. Here it should be noted that it is possible to make export even without displaying the dialog box. Like some sort of “quiet mode”. Then export will look like this:
where the first option - instance of the report and the second - the resulting file.
What we've got the result:
And the report itself:
Let's sum up. FastReport .Net surprised is with another interesting feature – creating a report from code. When might this be useful? If you do not want to produce a bunch of individual files with the report templates or want to hide a report template within the program in order to avoid damage or modify the template. It is also convenient to change the report template during the execution of your application. This gives great flexibility to the reports and the ability to use a single template, modifying it depending on the program logic.
I'm personally familiar and comfortable with using objects in the program code. Since the creation of the report is practically no different from writing basic code of application .