Crystal Reports Online Training

Learn Online, Anytime, Anywhere

Step-by-step online tutorials.

4.01 Sorting Records

In Chapter 2 you saw how to build a report using the different report objects and tie it into a database. For creating simple reports this is all you need to know. But you will quickly find yourself developing reports that require more effort than listing records one by one. For reports that consist of dozens, if not hundreds, of pages, providing a meaningful format that groups the data into logical units will go a long way towards making your reports easier to read. Crystal Reports .NET makes this possible by giving you the ability to sort and group data. Grouping reports also gives you the ability to create drill-down reports and summarize data. All of these features are covered in this chapter.

Sorting Records

Being able to sort records in either ascending or descending order is a reporting fundamental. Sorting makes it easy for a user to quickly find a particular piece of data buried inside many pages of data.

Reports can be sorted on a single field or on multiple fields. When sorting on multiple fields, you have to specify which field gets sorted first. When there are duplicate values for the first field, then the next field is used to resolve which record gets listed first. An example of this type of report is an employee report that sorts by name. The primary sort field is the last name. When there are duplicate last names, the secondary sort field, the first name, is used.

Using the Record Sort Order dialog box makes sorting records easy (shown in Figure 4-1). To use this dialog box, right-click on the report and select Report | Sort Records. On the left is the standard tree-view control listing the available fields. You can sort on fields from the report’s data sources or your own custom formulas.



Figure 4-1. The sort order dialog box.

To select a field, either drag and drop it to the Sort Fields window on the right or click on the arrow buttons to move it over. The order in which you add the fields determines which one gets priority in the sort order. The first field listed becomes the primary sort field. The next field is the secondary field, and so on. When there are duplicate values in one of the fields, then the next field on the list is used to resolve the conflict. This continues through all the sort fields as long as there are duplicates at each level.

Once you add a field to the Sort Fields window, you can’t change its ranking among the other fields. There are no up/down arrows that make it easy to reorder the list. You need to remove the fields that are out of order and then add them back in the proper order.

At the bottom of the dialog box is where you set whether the field is sorted in ascending or descending order. Each field is treated individually. First click on the field name and then click on the sort order.

As an example, let’s look at the example sort order shown in Figure 4-2. The primary sort field is the country. The secondary field is the region and this is followed by the customer name. This report produced by this dialog box is shown in Figure 4-2.



Figure 4-2. The multiple field sort report.

The report first lists all the countries that start with the letter “A”. The country Australia has multiple records, so the report performs a secondary sort on the Region field.

If you later determine that you need to change the sorting order, you can modify it using the same steps mentioned earlier. Just right-click on the report and select Report | Sort Records.