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.

August 12, 2024

How to build and install the Postgres plugin in FastReport .NET

This article describes how to connect to the database using the FastReport .NET plugin for the report designer from Visual Studio via the NuGet server.
August 08, 2024

How to install FastReport .NET and its components on Windows

Step-by-step instructions for online and manual installation via the FastReport registration code.NET and its components in Windows.
July 26, 2024

Updating HTMLObject as a plugin for FastReport .NET

Detailed instructions for using the new HTMLObject plugin, which uses splitting DOM HTML into FastReport report objects.
Fast Reports
  • 800-985-8986 (English, US)
  • +4930568373928 (German)
  • +55 19 98147-8148 (Portuguese)
  • info@fast-report.com
  • 901 N Pitt Str #325 Alexandria VA 22314

© 1998-2024 Fast Reports Inc.