Skip to main content

DxGrid.SortBy(String, GridColumnSortOrder) Method

Sorts data by column values in the specified order.

Namespace: DevExpress.Blazor

Assembly: DevExpress.Blazor.v24.1.dll

NuGet Package: DevExpress.Blazor

Declaration

public void SortBy(
    string columnFieldName,
    GridColumnSortOrder sortOrder
)

Parameters

Name Type Description
columnFieldName String

Specifies the name of the data source field that supplies data for the processed column. If the component does not contain the column that corresponds to the specified data field, an exception occurs.

sortOrder GridColumnSortOrder

The column’s sort order.

Remarks

The SortBy method allows you to sort grid data in code regardless of the DxGrid.AllowSort or DxGridDataColumn.AllowSort property value.

The following code snippet uses different method overloads.

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

<DxGrid Data="GridDataSource"
        @ref="MyGrid">
    <Columns>
        <DxGridDataColumn FieldName="OrderDate"
                          DisplayFormat="d"/>
        <DxGridDataColumn FieldName="ShipName" />
        <DxGridDataColumn FieldName="ShipCity" />
        <DxGridDataColumn FieldName="Freight"
                          DisplayFormat="n2"
                          SortIndex="0"/>
    </Columns>
</DxGrid>

<DxButton Click="@(() => MyGrid.SortBy("OrderDate"))">Sort By Order Date</DxButton>
<DxButton Click="@(() => MyGrid.SortBy("OrderDate", GridColumnSortOrder.Descending))">Sort By Order Date (Descending Order)</DxButton>
<DxButton Click="@(() => MyGrid.SortBy("OrderDate", GridColumnSortOrder.Descending, 0))">Sort By Order Date (Descending Order, SortIndex = 0)</DxButton>

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

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

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

Grid - Sort By Amount

Watch Video: Grid - Sort Data

For more information about data sorting in the Grid component, refer to the following topic: Sort Data in Blazor Grid.

See Also