Let's remember a fairly common situation when you need to do something very quickly and using whatever is available. But let's not forget that the structure and content of the report depend on external factors. The FastReport.NET report generator is a very flexible product and gives you two ways to solve this problem.
Method 1. To create a report structure in the code of a custom application.
Method 2. To control the behavior of the report inside, using a script.
There is quite a lot of information on how to implement certain behavior features in a report script. But quite a few examples show how to create a report from code. In this article, we will create a report with a table from the application code, and we’ll also look at how to fill a table with data and place an object in a table cell. Method 2 will be described in more detail in another article.
We can fill the Table object with static or dynamic data. In the first case, we know the specific dimensions of the table and enter the data into the cells from the fixed set straight away.
In the second case, the table is created dynamically, rows (columns) are added depending on the data in the source. However, you can also limit the size of the table and fix it.
Let's create a WinForms application. Let's include the FastReport.dll library in the links, which you can find in the folder with the installed report generator. Let's add a button to the form that will start building the report. Don't forget to create for it a click handler.
First of all, we include the FastReport libraries.
Now let's look at the example when we need to fill the table with data from the source. Let's replace the loop above with another code:
In the end, we will get a table with data from the database:
The table looks incomplete without a title. Let's add it:
Let me clarify that we specified the titles in the first row of the table, which means that the cycle begins not from the first, but the second element.
Let's look at the last case for today - how to place an object in a table cell. For example, a picture:
It is the Parent property that affects the way the picture will be displayed in the cell. Let's see how it will look:
Thus, we’ve looked at how to create a table in the report from the application code and two ways to fill it with static or dynamic data. In addition, now you know how to programmatically add an object to a table cell.