Skip to main content

GridCustomSortEventArgs Class

Contains data for the CustomSort event.

Namespace: DevExpress.Blazor

Assembly: DevExpress.Blazor.v23.2.dll

NuGet Package: DevExpress.Blazor

Declaration

public class GridCustomSortEventArgs

Remarks

Follow the steps below to implement custom logic used to sort data in the DxGrid.

  1. Set the SortMode property to Custom.
  2. Handle the CustomSort event to implement your logic that compares column values. When the event fires, the component compares two row values: the Value1 and Value2 properties. To define the column’s sort algorithm, set the Result property to one of the following values:

    Value Description
    -1 The grid places the first data item above the second item in ascending sort mode and below the second item in descending sort mode.
    1 The grid places the first data item below the second item in ascending sort mode and above the second item in descending sort mode.
    0 Indicates that the data items are equivalent according to the comparison condition. The grid sorts them based on their indices in the data source.
  3. Set the Handled property to true to indicate that the current comparison operation is handled. If the value is set to false, the component ignores the result of the custom comparison and uses the default mechanism to compare item values.

Note

The Grid does not support custom sorting when you use a Server Mode data source or GridDevExtremeDataSource.

The code below implements custom logic to sort the Order Date column’s values without taking into account the year part of the date.

@using Microsoft.EntityFrameworkCore
@inject IDbContextFactory<NorthwindContext> NorthwindContextFactory
@implements IDisposable

<DxGrid Data="GridDataSource"
        CustomSort="OnCustomSort">
    <Columns>
        <DxGridDataColumn FieldName="OrderDate"
                          DisplayFormat="d"
                          SortIndex="1"
                          SortMode="GridColumnSortMode.Custom"/>
        <DxGridDataColumn FieldName="ShipName" />
        <DxGridDataColumn FieldName="ShipCity" />
        <DxGridDataColumn FieldName="Freight"
                          DisplayFormat="n2"/>
    </Columns>
</DxGrid>

@code {
    object GridDataSource { get; set; }
    NorthwindContext Northwind { get; set; }

    protected override void OnInitialized() {
        Northwind = NorthwindContextFactory.CreateDbContext();
        GridDataSource = Northwind.Orders.ToList();
    }

    void OnCustomSort(GridCustomSortEventArgs e) {
        if(e.FieldName == "OrderDate") {
            var date1 = Convert.ToDateTime(e.Value1);
            var date2 = Convert.ToDateTime(e.Value2);

            if(date1.Month == date2.Month)
                e.Result = date1.Day.Compare(date2.Day);
            else
                e.Result = date1.Month.Compare(date2.Month);
            e.Handled = true;
        }
    }

    public void Dispose() {
        Northwind?.Dispose();
    }
}

Grid - Custom Sorting

Watch Video: Grid - Sort Data

View Example: Grid - Custom Sorting

Inheritance

Object
GridCustomSortEventArgs
See Also