How to make an interactive report with toggle sorting

2021-06-02

FastReport .NET

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.

FastReport .NET.

The report has a configured sorting:

FastReport .NET.

 

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.

FastReport .NET.

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.

FastReport .NET.

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.

FastReport .NET.

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:

bool sorting = false;

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:

 private void Text1_Click(object sender, EventArgs e)
 {
 Data1.Sort[0].Descending = sorting;
 sorting = !sorting;
 Report.Refresh();
 }

If we launch the report and click the title – “EMPLOYEES” – we will see the following:

FastReport .NET.

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:

 private void Text1_Click(object sender, EventArgs e)
 {
 Data1.Clear();
 Data1.Sort.Add(new Sort("[Employees.FirstName]", false));
 Data1.Sort.Add(new Sort("[Employees.LastName]", sorting));
 sorting = !sorting;
 Report.Refresh();
 } 

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.

November 20, 2024

Localization and Language Switching in FastReport VCL

FastReport VCL supports 40 languages for interface localization and allows you to change the language on the fly through menus or code, without recompilation.
November 01, 2024

New Features of the FastReport VCL Editor

We are considering new features of the report editor: extension lines, highlighting of intersecting objects, updated report and data trees.
October 30, 2024

Using Styles When Creating Reports in FastReport VCL

The article discusses one of the new features of FastReport VCL — the use of styles and style sheets.
Fast Reports
  • 800-985-8986 (English, US)
  • +4930568373928 (German)
  • +55 19 98147-8148 (Portuguese)
  • info@fast-report.com
  • 66 Canal Center Plaza, Ste 505, Alexandria, VA 22314

© 1998-2024 Fast Reports Inc.