How to: Sort Data by Columns (Rows) in Code

In a PivotGridControl, values of a specific row field can be sorted by values in a grand total column or any other column. Similarly, values of a specific column field can be sorted by values in a grand total row, or any other. The following example shows how to sort a row field's data by column values. To sort data, the PivotGridFieldBase.SortBySummaryInfo property is used.

Consider the following layout:

sortbycolumn_initiallayout

  1. To sort values of the Product Name row field by values of the "Grand Total" column calculated against the Extended Price data field, use the following code:

    
    fieldProductName.SortBySummaryInfo.Conditions.Clear();
    fieldProductName.SortBySummaryInfo.Field = fieldExtendedPrice;
    

    The result is displayed below:

    sortbycolumn_sortbygrandtotal

  2. To sort values of the Product Name row field by values of the "1994 Total" column calculated against the Extended Price data field, use the following code:

    
    fieldProductName.SortBySummaryInfo.Field = fieldExtendedPrice;
    fieldProductName.SortBySummaryInfo.Conditions.Clear();
    fieldProductName.SortBySummaryInfo.Conditions.Add(new PivotGridFieldSortCondition(fieldOrderDate, 1994));
    

    The result is displayed below:

    sortbycolumn_sortbytotal

  3. To sort values of the Product Name row field by values of the "1994 - USA" column calculated against the Extended Price data field, use the following code:

    
    fieldProductName.SortBySummaryInfo.Field = fieldExtendedPrice;
    fieldProductName.SortBySummaryInfo.Conditions.Clear();
    fieldProductName.SortBySummaryInfo.Conditions.Add(new PivotGridFieldSortCondition(fieldOrderDate, 1994));
    fieldProductName.SortBySummaryInfo.Conditions.Add(new PivotGridFieldSortCondition(fieldCountry, "USA"));
    

    The result is displayed below:

    sortbycolumn_sortbycolumn