[Expand]General Information
[Collapse]WinForms Controls
  .NET Core Support
 [Expand]What's Installed
 [Expand]Build an Application
 [Collapse]Controls and Libraries
  [Expand]Forms and User Controls
  [Expand]Messages, Notifications, and Dialogs
  [Expand]Editors and Simple Controls
  [Expand]Ribbon, Bars and Menu
  [Expand]Application UI Manager
  [Expand]Docking Library
  [Expand]Data Grid
  [Expand]Vertical Grid
  [Expand]Property Grid
  [Expand]Pivot Grid
  [Expand]Tree List
  [Expand]Gantt Control
  [Expand]Chart Control
  [Expand]Map Control
   [Expand]Product Information
    Product Structure
    Getting Started
   [Expand]Spreadsheet Document
    Supported Formats
   [Expand]Cell Basics
    Defined Names
    Data Binding
    Data Validation
   [Expand]Data Presentation
    Data Grouping
   [Expand]Pivot Tables
   [Expand]Charts and Graphics
   [Expand]Mail Merge
    Operation Restrictions
    Find and Replace
   [Expand]Visual Elements
   [Expand]Keyboard Shortcuts
    [Expand]Rows and Columns
    [Expand]Import and Export Data
    [Expand]Data Binding
    [Collapse]Mail Merge
      How to: Perform a Mail Merge
      How to: Create a Master-Detail Report
      How to: Sort Data
      How to: Group Data
      How to: Filter Data
      How to: Create a Master-Detail Report Without Coding
    [Expand]Formatting Cells
    [Expand]Conditional Formatting
    [Expand]Group Data
    [Expand]Filter Data
    [Expand]Pivot Tables
  [Expand]Rich Text Editor
  [Expand]Spell Checker
  [Expand]Form Layout Managers
  [Expand]Navigation Controls
  [Expand]PDF Viewer
  [Expand]TreeMap Control
  [Expand]Sunburst Control
 [Expand]Common Features
  Get More Help
 [Expand]API Reference
[Expand]ASP.NET Controls and MVC Extensions
[Expand]ASP.NET Bootstrap Controls
[Expand]ASP.NET Core Bootstrap Controls
[Expand]WPF Controls
[Expand]Xamarin Controls
[Expand]Windows 10 App Controls
[Expand]Office File API
[Expand]Report and Dashboard Server
[Expand]eXpressApp Framework
[Expand]eXpress Persistent Objects
[Expand]CodeRush Classic
[Expand]Cross-Platform Core Libraries
[Expand]Tools and Utilities
 End-User Documentation
View this topic on docs.devexpress.com (Learn more)

How to: Sort Data

When performing a Mail Merge with the SpreadsheetControl, you can specify the sort order in which data entries will appear in the resulting document. This document provides an example on how to specify sort criteria for multi-level detail data.

  1. Prepare the Application
  2. Sort Top-Level Data
  3. Sort Detail-Level Data
  4. How Sort Criteria are Saved in a Template
  5. Get the Result

Expanded Prepare the Application

To get started with this tutorial, run the application created in the How to: Create a Master-Detail Report example. A master-detail template is automatically loaded into the SpreadsheetControl. This template contains a two-level detail range (click the Mail Merge Design View button in the Ribbon to show template ranges):

  • Detail - the detail range that displays top-level data from the Categories table of the sample Northwind database.
  • DetailLevel0 - the nested-level detail range that displays data from the CategoriesProducts child table.

In each detail range, you can sort data by fields of the associated data member. In this example, sort top-level data by category name alphabetically, and specify two sort criteria for detail-level data - by product unit price in descending order (primary), and by product name in ascending order (secondary).

Expanded Sort Top-Level Data

  1. Select any cell within the Detail range (but outside of the DetailLevel0 nested range), and click the Sort Fields button on the Mail Merge tab, in the Sort & Group group. This invokes the Sort Fields dialog.


    Note that the sorting feature is not available if a mail merge template is not bound to a data source or if the currently selected cell in the template is outside the detail range.

  2. In the dialog, click New and select the CategoryName data field to be used as a criterion for sorting categories.


    Note that the Sort Fields dialog invoked for the selected detail range (for a top-level or nested-level detail range) allows you to only choose the fields of the data member associated with this detail range.

  3. Define the Ascending sort order.

Expanded Sort Detail-Level Data

  1. Click anywhere within the DetailLevel0 range and invoke the Sort Fields dialog.
  2. Click New and select the UnitPrice data field to be used as the primary criterion to sort the products in a category. Set the Descending sort order.
  3. Click New and select the ProductName data field to be used as a secondary criterion to sort category products. Set the Ascending sort order.

    You can adjust the priority of the sort criteria, if necessary, using the Up and Down buttons in the dialog.

Expanded How Sort Criteria are Saved in a Template

A sort field specified for a detail range in the mail merge template is a "SORTFIELD(n)" defined name, where (n) is the zero-based index of the sort field (sort fields are numbered consecutively throughout the template). The name of the data field specified as a sort criterion is assigned to the DefinedName.RefersTo property of the defined name, and the sort order is set to DefinedName.Comment.

A defined name that corresponds to a detail range holds all sort fields specified for this detail range in its DefinedName.Comment property (associated sort field names are divided by semicolon).

For example, after you have specified sort fields as described above, the following defined names are added...

Name RefersTo Comment
SORTFIELD0 "CategoryName" Ascending
SORTFIELD1 "UnitPrice" Descending
SORTFIELD2 "ProductName" Ascending

... and these sort fields are listed in the DefinedName.Comment property value of the defined names that specify detail ranges.

Name RefersTo Comment

Defined names specifying sort fields are added to the Worksheet.DefinedNames collection of the template worksheet.

Expanded Get the Result

Click the Mail Merge Preview button to review the result.

Expanded See Also

Is this topic helpful?​​​​​​​