Occasionally, our users face the task of making an interactive report. It may be a report where the order of data sorting can be changed after querying. Today, we will consider the process of creating such a report.
Let us assume that we have a ready file with configured sorting. As an example, we will take a Simple List report from the FastReport .NET demo application.
The report has a configured sorting:
First, the bands are sorted by name, then by surname. Sorting is carried out in the ascending order, that is, from A to Я in the case of the Cyrillic font, or from A to Z in the case of the Roman one.
Now we add interactivity to our report. Select a text object – a title, for example, and add an event handler Click. Thus, after clicking on the object, the preview shows the function, which we will configure.
Also, we change the Cursor property to Hand, so that the cursor changes into a hand when pointing at an object. Thus it becomes apparent that the object is clickable.
Let us see how sorting works in FastReport so that we are able to write the function code.
Data sorting is stored as a collection of values. There are several options to implement sort changing, but all of them are reduced to modifying this collection.
If we look at sorting in the code, we will see a list of methods and properties. We will work with the properties Descending and Expression.
The Expression property coincides with the “Sort by…” field in the designer, while the Descending property coincides with the “ascending/descending order” toggle. Note that only three sorting rules can be configured from the designer, while an unlimited number of them can be added from the code. Accordingly, the designer does not support more than three rules. When opening the band properties with four saved rules, the first three of them will be shown, and only they will be saved after changing.
The order of sorting rules begins with the rule with the index 0; then the rule with the index 1 is applied, and so on.
In our case, the Sort collection has two values:
1) Expression = [Employees.FirstName], Descending = false
2) Expression = [Employees.LastName], Descending = false
Now, we start writing the code.
We add the “sorting” variable, which will set the order of sorting:
To change the order of sorting, one has to change the Descending property. We will change it for the zero element of the Sort collection, then the Sorting is inverted and the report is updated:
If we launch the report and click the title – “EMPLOYEES” – we will see the following:
As you can see, sorting was inverted; the final records are now in the first positions. It is worth noting that the Sort collection can be not only modified, but also its elements can be added or removed. For example, the sort change can be done in a different way:
In this code, we clear the collection and add two new sorting rules into it, one of them with a changeable property.
Thus, we have examined how sorting can be changed from the script. Besides changing the sorting by a click, you may use the above functions in combination with others. For example, you may change a band sorting by clicking a button in a dialogue tab, making an interactive list of fields of a data source, or change sorting depending on any other value.