.NET Framework 4.5.2+
.NET Framework 4.5.2+
.NET Standard 2.0+
Row

PivotField.SortItems(PivotFieldSortType, Int32, PivotItemReference[]) Method

Sorts items in the current pivot field based on values in the specified data field.

Namespace: DevExpress.Spreadsheet

Assembly: DevExpress.Spreadsheet.v21.2.Core.dll

Declaration

void SortItems(
    PivotFieldSortType sortType,
    int dataFieldIndex,
    params PivotItemReference[] itemReferences
)

Parameters

Name Type Description
sortType PivotFieldSortType

One of the PivotFieldSortType enumeration values specifying the sort order.

dataFieldIndex Int32

An integer value that is the zero-based index of the data field in the PivotDataFieldCollection collection to sort by.

itemReferences PivotItemReference[]

An array of PivotItemReference objects representing references to the report’s items that specify a row or column in the data area on which to base sorting.

Remarks

Use the SortItems method to sort items in a row or column field by data field values. The PivotItemReference objects allow you to specify references to specific items in the report that describe a particular row or column in the data area containing values to sort by.

The following example demonstrates how to sort products in descending order by sales values in the first quarter.

// Access the pivot table by its index in the collection.
PivotTable pivotTable = worksheet.PivotTables[0];

// Create a reference to the "Q1" item of the "Quarter" field.
PivotItemReference item = new PivotItemReference(0, 0);
// Sort the "Product" field by the "Sum of Amount" field in descending order using values in the "Q1" column.
pivotTable.Fields["Product"].SortItems(PivotFieldSortType.Descending, 0, item);

The following image shows the result of the code’s execution.

PivotField_Sorting_SortByDataColumn

If the itemReferences parameter is not specified, items in the field will be sorted by their grand total values.

If you try to apply sorting to a field that is not currently shown as a row or column field in the report, or the sort settings you specify are invalid, an exception occurs. To view or modify sorting criteria, use the PivotField.Sort property.

To sort items in a specific field in ascending or descending order, use the PivotField.SortType property.

See Also