Sort Data and Display Top Rows

Sorting Data

Among the main benefits of a pivot report is its ability to provide summarized data against matching values instead of viewing each and every record in which the corresponding value appears. The ExpressPivotGrid always sorts values in its axes. It does this to merge matching values into a single value. Thus, you get a single entry for the same trademark, model, date, person or whatever information you are analyzing.

By default, the values in the row and column axes are sorted in ascending order. If values are displayed hierachically, the values in the child groups are also sorted.

You can change the sort order for any field by toggling the field's SortOrder property. End-users can do the same by clicking the desired field header. This feature makes it much easier for end-users to find the desired information and lets them adjust the flow of data before printing a report.

If you are implementing sophisticated data analysis sub-systems, default sorting routines may not suit your needs. For instance, you may wish to sort alpha-numeric strings in a specific order. In such cases, you can easily implement your own custom sorting logic. For this purpose, handle the control's OnCompare event.

Sorting by Summary Values

With sorted values, end-users can navigate through reports much more easily and find the entries they are interested in. But often the reverse is required, instead of searching say for how many sales there have been of BMWs, end-users may instead wish to find the brand which has sold the most or least. Instead of searching for how many goals a particular player has scored, they may wish to find the player with the lowest or highest goal count.

The ExpressPivotGrid lets you easily solve such reverse data analysis tasks because each field can be sorted by the corresponding grand total values instead of its own values. All you need to do to make this happen is to specify the field whose summaries should be used for sorting. The following image shows how you can sort players by the corresponding steal count. And like the default sort mechanism, end-users can still freely manipulate the sort order by clicking headers.

There is one more thing you can do to make a report much more usable. You can allow end-users to choose which field is used to sort values. Since only a single property change is required to specify the target field, you only need a few lines of code to implement this. Consider an example where a combo box is used to switch between sort fields. To adopt such a combo box for the above report, you'll need the following handler to respond to changing the selected item.


procedure TForm1.ComboBox1Select(Sender: TObject);
 SortFields: array[0..3] of string = ('', 'Points', 'Rebounds', 'Steals');
  S: string;
  S := SortFields[ComboBox1.ItemIndex];
  FieldName.SortBySummaryInfo.Field := cxDBPivotGrid1.GetFieldByName(S);

The following image shows the combo box in action.

Top N Values Report

The ExpressPivotGrid provides yet another feature that can greatly enhance your reports. If end-users are not interested in browsing the entire report but only need to investigate the best or worst results, you can suppress any surplus information. To do this, you can specify the number of results that are displayed for each field. Thus, end-users will see the specified number of best results if a field is sorted in descending order. Switching the sort order will result in displaying the same number of worst results.

Again, only a single property's value needs to be changed to enable this feature. And only a single line of code is required to allow end-users to control this. For instance, consider the following value change handler for a spin editor.


procedure TForm1.UpDown1Click(Sender: TObject; Button: TUDBtnType);
  FieldName.TopValueCount := UpDown1.Position;

As a result, end-users obtain full control over the data analysis process and can manipulate data layout as they require. The following image shows a more compact report that has been built by limiting the number of top values displayed on-screen.

At times you may need to inform end-users of how the best or worst results displayed on-screen relate to the rest of their data. End-users may need to know how many points have been scored by the rest of the team or how many sales have been completed for other car trademarks, etc. You can display this information by setting the sorted field's TopValueShowOthers property to True. The following image displays an example wherein a pivot grid shows yearly car sales data. Each year displays the 3 best-selling models and information about the sales by the remaining models.

Back to the Feature List


Chat is one of the many ways you can contact members of the DevExpress Team.
We are available Monday-Friday between 7:30am and 4:30pm Pacific Time.

If you need additional product information, write to us at or call us at +1 (818) 844-3383


DevExpress engineers feature-complete Presentation Controls, IDE Productivity Tools, Business Application Frameworks, and Reporting Systems for Visual Studio, along with high-performance HTML JS Mobile Frameworks for developers targeting iOS, Android and Windows Phone. Whether using WPF, Silverlight, ASP.NET, WinForms, HTML5 or Windows 8, DevExpress tools help you build and deliver your best in the shortest time possible.

Copyright © 1998-2014 Developer Express Inc.
All trademarks or registered trademarks are property of their respective owners