Skip to main content
A newer version of this page is available. .

PivotGridSettings.CustomFieldSort Property

Provides the ability to sort data using custom rules.

Namespace: DevExpress.Web.Mvc

Assembly: DevExpress.Web.Mvc5.v20.2.dll

NuGet Package: DevExpress.Web.Mvc5

Declaration

public PivotGridCustomFieldSortEventHandler CustomFieldSort { get; set; }

Property Value

Type Description
PivotGridCustomFieldSortEventHandler

A PivotGridCustomFieldSortEventHandler delegate method allowing you to implement custom processing.

Remarks

Handle the CustomFieldSort event to provide a custom sorting algorithm for a specific field. Note that the PivotGridFieldBase.SortMode property of this field should be set to PivotSortMode.Custom to apply custom sorting. Otherwise, the CustomFieldSort event will not be raised.

The CustomFieldSort event fires for pairs of rows in the underlying data source, before data is grouped according to the layout of column and row fields. The PivotGridCustomFieldSortEventArgs.ListSourceRowIndex1 and PivotGridCustomFieldSortEventArgs.ListSourceRowIndex2 parameters identify the indexes of the rows. The field being processed is specified by the PivotGridCustomFieldSortEventArgs.Field parameter. The field’s values in these rows are specified by the PivotGridCustomFieldSortEventArgs.Value1 and PivotGridCustomFieldSortEventArgs.Value2 parameters.

In the event handler, you should compare these two values and assign the result to the PivotGridCustomFieldSortEventArgs.Result property as follows:

  • set Result to -1, if the first row should be positioned above the second row, when sorting data in ascending order. When data is sorted in descending order, the first row will be positioned below the second row.
  • set Result to 1, if the first row should be positioned below the second row, when sorting data in ascending order. When data is sorted in descending order, the first row will be positioned above the second row.
  • set Result to 0 to indicate that the rows are equal. In this instance, the rows will be arranged according to their indexes in the data source.

If your comparison logic requires additional data from the underlying data source, use the PivotGridCustomFieldSortEventArgs.GetListSourceColumnValue method. This method allows you to obtain values from the data source by a column name and row index.

The PivotGridCustomFieldSortEventArgs.Handled property must be set to true, if the current comparison operation was handled. You can leave this parameter set to false, to invoke the default comparison mechanism after your event handler has finished. In the latter instance, the result of a custom comparison operation is ignored.

The CustomFieldSort event also fires to sort unique filter values in the Filter Window. Each filter value might correspond to multiple rows in the underlying data source. So, the event’s ListSourceRowIndex1 and ListSourceRowIndex2 parameters cannot be used to identify underlying rows. They are set to -1, when comparing filter values in the Filter Window.

Note

Custom sorting is not supported in OLAP or server mode. To sort data in OLAP and server mode using custom algorithms, handle the ASPxPivotGrid.CustomServerModeSort event.

Example

This example demonstrates how to sort a field (Sales_Person) by the hidden data field values (LastName).

Check the Enable Custom Sorting box to sort the Sales_Person values by LastName instead of the default alphabetical sorting order.

The enabled check box switches the PivotGridField.SortModeProperty property to the Custom value. This setting allows the PivotGridControl to fire the CustomFieldSort event for that field using the PivotGridSettings.CustomFieldSort property.

@{
    ViewBag.Title = "Home Page";
}
<script>
    function onCheckedChanged(s, e) {
        PivotGrid.PerformCallback({ enable: s.GetChecked() });
    }
</script>


@Html.DevExpress().CheckBox(settings => {
    settings.Name = "CheckBox";
    settings.Checked = false;
    settings.Text = "Enable Custom Sorting";
    settings.Properties.ClientSideEvents.CheckedChanged = "onCheckedChanged";
}).GetHtml()

@Html.Action("PivotGridPartial")
See Also