Skip to main content
A newer version of this page is available. .

DxGrid.SortBy(String, GridColumnSortOrder, Int32) Method

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

Namespace: DevExpress.Blazor

Assembly: DevExpress.Blazor.v21.1.dll

NuGet Package: DevExpress.Blazor

Declaration

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

Parameters

Name Type Description
fieldName String

Specifies the name of the data source field that supplies data for the sorted column.

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
@using Grid.Northwind
@inject NorthwindContext Northwind

<DxGrid Data="GridDataSource"
        CustomizeCellDisplayText="OnCustomizeCellDisplayText"
        UnboundColumnData="OnUnboundColumnData"
        @ref="MyGrid">

    <Columns>
        <DxGridDataColumn FieldName="OrderDate" DisplayFormat="d" />
        <DxGridDataColumn FieldName="Customer" />
        <DxGridDataColumn FieldName="Freight" DisplayFormat="n2" SortIndex="0" />
        <DxGridDataColumn FieldName="Amount" UnboundType="GridUnboundColumnType.Decimal" DisplayFormat="c" />
    </Columns>
</DxGrid>

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

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

    protected override void OnInitialized() {
        GridDataSource = Northwind.Orders
            .Include(i => i.Customer)
            .Include(i => i.OrderDetails)
            .Include(i => i.ShipViaNavigation)
            .ToList();
    }

    void OnCustomizeCellDisplayText(GridCustomizeCellDisplayTextEventArgs e) {
        if (e.FieldName == "Customer") {
            var customer = (Customer)e.Value;
            e.DisplayText = $"{customer.CompanyName} ({customer.Country}, {customer.City})";
        }
    }

    void OnUnboundColumnData(GridUnboundColumnDataEventArgs e) {
        if (e.FieldName == "Amount") {
            var details = (ICollection<OrderDetail>)e.GetRowValue("OrderDetails");
            e.Value = details.Sum(i => i.Quantity * i.UnitPrice * (1 - (decimal)i.Discount));
        }
    }
}

Grid - Sort By Amount

See Also