Sometimes with some data sources we have to create aliases for the table fields. This happens when the field names are not understood intuitively or their native language is not used in their naming. For the convenience of working with such a database, FastReport .Net allows you to create aliases. But it is inconvenient to create them manually since every time you have to create a new report. So I'll show you how to create aliases in the code. This allows you to use this data source in multiple reports.
To demonstrate it, I'll create an application with German localization. That is, the interface and the field names are in German.
Keep in mind that data source with aliases should be accessible when you create a report using the File-> New menu. In order to do this, we need listen for the event of selection menu New item.
Let's use a Windows Forms application.
Add two buttons to the form.
The first button launches the designer with a blank report. Second - opens a demo account in the designer.
Add the libraries:
First of all, create a procedure for registering a data source for the report:
As you can see, we have created a DataSet. Then, we loaded XML database into it. Then, registered the data source in the report. In the cycle in the data source we find the “Employees “ table. Enable it (Enabled = true). We made it for the table to appear in the designer’s data window. Now we assign an alias to each field of the table - the name of the German language.
Handling the designer load event:
Set localization language in the designer resources - in this case German.
Then assign the event handler DesignerLoaded to our own event, which we write below.
Write a handler for DesignerLoaded:
Here, we assign the event of New menu selection our handler. This is a way of intercepting the event.
We write the handler to select the menu item New:
Here we use a blank designer. Create an instance of Blank Report Wizard. As the name implies – it’s a blank report. Then we run the designer, registering a data source and update the data tree in the data window.
It's time to create handlers for the two buttons. The button of creation a new report:
Here, the report object is created. The data is recorded for it. Run the designer.
The button of report editing. Basically, it’s the same thing, but add the loading of existing report:
Do not forget that in the folder with the executable file there should be: the report, the database file and the locale (German.frl).
Run the application. Click on the second button. We get the report template. Let us open a data source in the tree:
All fields, as well as the table name, are displayed in German, thanks to aliases.
Now, create a new blank report using the File-> New menu.
Once again, we see our data source alias.
We have created a data source and assigned aliases to the table fields. Now we can use this source in this form in any report.
By using aliases it is much easier to create reports for people who are not familiar with the field names in a particular database. You can use aliases only once in the database, and get rid of the many questions from the report developers.
And lastly the report itself: