Skip to main content

DxGrid.SortBy(String, GridColumnSortOrder, Int32) Method

Sorts data by column values in the specified order, and specifies the sorted column’s index.

Namespace: DevExpress.Blazor

Assembly: DevExpress.Blazor.v23.2.dll

NuGet Package: DevExpress.Blazor

Declaration

public void SortBy(
    string columnFieldName,
    GridColumnSortOrder sortOrder,
    int sortIndex
)

Parameters

Name Type Description
columnFieldName String

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

sortOrder GridColumnSortOrder

An enumeration value that specifies the column’s sort order.

sortIndex Int32

An integer value that specifies the sorted column’s index (zero-based) among other sorted columns.

Remarks

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

The code below demonstrates how to use 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