Skip to main content

DxGrid.CustomSort Event

Allows you to implement custom logic used to sort data in the grid.

Namespace: DevExpress.Blazor

Assembly: DevExpress.Blazor.v21.2.dll

Declaration

[Parameter]
public Action<GridCustomSortEventArgs> CustomSort { get; set; }

Parameters

Type Description
GridCustomSortEventArgs

Provides access to grid data.

Remarks

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

  • Set the DxGridDataColumn.SortMode property to GridColumnSortMode.Custom.
  • Handle the DxGrid.CustomSort event to implement your logic that compares column values. Use the GridCustomSortEventArgs event arguments (Value1, Value2, FieldName, and so on) to access column values and other grid data.

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") {
            e.Handled = true;
            int month1 = Convert.ToDateTime(e.Value1).Month;
            int month2 = Convert.ToDateTime(e.Value2).Month;
            if (month1 > month2)
                e.Result = 1;
            else if (month1 < month2)
                e.Result = -1;
            else e.Result = System.Collections.Comparer.Default.Compare(Convert.ToDateTime(e.Value1).Day,
                                    Convert.ToDateTime(e.Value2).Day);
        }
    }

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

Grid - Custom Sorting

Watch Video: Grid - Sort Data

See Also