DxGrid.CustomSort Event
Allows you to implement custom logic used to sort data in the grid.
Namespace: DevExpress.Blazor
Assembly: DevExpress.Blazor.v24.2.dll
NuGet Package: DevExpress.Blazor
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 SortMode property to
Custom
. 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. Set the Handled property to
true
to indicate that the current comparison operation is handled. If the value is set tofalse
, 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 following code snippet 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();
}
}
For more information about data sorting in the Grid component, refer to the following topic: Sort Data in Blazor Grid.