Blazorise DataGrid: Sorting
Sorting allows you to arrange data in ascending or descending order. To sort a column, click its header.
Overview
All columns can be sorted automatically if the option Sortable
is enabled on the column.
Use SortField
if you would like to set a different field or property to be considered by the sorting mechanism on a certain column.
Examples
Single
By default the DataGrid sorting mode is single column based.<DataGrid TItem="Employee" Data="" @bind-SelectedRow="" Responsive Sortable SortMode="DataGridSortMode.Single"> <DataGridCommandColumn /> <DataGridColumn Field="@nameof(Employee.Id)" Caption="#" Sortable="false" /> <DataGridColumn Field="@nameof(Employee.FirstName)" Caption="First Name" Editable /> <DataGridColumn Field="@nameof(Employee.LastName)" Caption="Last Name" Editable /> <DataGridColumn Field="@nameof(Employee.Email)" Caption="Email" Editable /> <DataGridColumn Field="@nameof(Employee.Salary)" Caption="Salary" DisplayFormat="{0:C}" DisplayFormatProvider="@System.Globalization.CultureInfo.GetCultureInfo("fr-FR")" Editable> <EditTemplate> <NumericEdit TValue="decimal" Value="@((decimal)context.CellValue)" ValueChanged="@( v => context.CellValue = v)" /> </EditTemplate> </DataGridColumn> </DataGrid>
@code{ [Inject] public EmployeeData EmployeeData { get; set; } private List<Employee> employeeList; private Employee selectedEmployee; protected override async Task OnInitializedAsync() { employeeList = await EmployeeData.GetDataAsync(); await base.OnInitializedAsync(); } }
Multiple
You may also change the DataGrid sorting mode to multiple, to allow sorting on multiple columns.<DataGrid TItem="Employee" Data="" @bind-SelectedRow="" Responsive Sortable SortMode="DataGridSortMode.Multiple"> <DataGridCommandColumn /> <DataGridColumn Field="@nameof(Employee.Id)" Caption="#" Sortable="false" /> <DataGridColumn Field="@nameof(Employee.FirstName)" Caption="First Name" Editable /> <DataGridColumn Field="@nameof(Employee.LastName)" Caption="Last Name" Editable /> <DataGridColumn Field="@nameof(Employee.Email)" Caption="Email" Editable /> <DataGridColumn Field="@nameof(Employee.Salary)" Caption="Salary" DisplayFormat="{0:C}" DisplayFormatProvider="@System.Globalization.CultureInfo.GetCultureInfo("fr-FR")" Editable> <EditTemplate> <NumericEdit TValue="decimal" Value="@((decimal)context.CellValue)" ValueChanged="@( v => context.CellValue = v)" /> </EditTemplate> </DataGridColumn> </DataGrid>
@code{ [Inject] public EmployeeData EmployeeData { get; set; } private List<Employee> employeeList; private Employee selectedEmployee; protected override async Task OnInitializedAsync() { employeeList = await EmployeeData.GetDataAsync(); await base.OnInitializedAsync(); } }
SortField
The SortField
feature, allows you to define a different Property or Field of the Item to be considered by the sorting mechanism.
In this example, we define the Sort Field of the Childrens
Column to be the calculated property ChildrensPerSalary
.
<DataGrid TItem="Employee" Data="" @bind-SelectedRow="" Responsive Sortable SortMode="DataGridSortMode.Single"> <DataGridCommandColumn /> <DataGridColumn Field="@nameof(Employee.Id)" Caption="#" Sortable="false" /> <DataGridColumn Field="@nameof(Employee.FirstName)" Caption="First Name" Editable /> <DataGridColumn Field="@nameof(Employee.LastName)" Caption="Last Name" Editable /> <DataGridColumn Field="@nameof(Employee.Email)" Caption="Email" Editable /> <DataGridColumn Field="@nameof(Employee.Salary)" Caption="Salary" DisplayFormat="{0:C}" DisplayFormatProvider="@System.Globalization.CultureInfo.GetCultureInfo("fr-FR")" Editable> <EditTemplate> <NumericEdit TValue="decimal" Value="@((decimal)context.CellValue)" ValueChanged="@( v => context.CellValue = v)" /> </EditTemplate> </DataGridColumn> <DataGridNumericColumn TItem="Employee" Field="@nameof( Employee.Childrens )" Caption="Childrens" Editable Filterable="false" SortField="@nameof( Employee.ChildrensPerSalary )" SortDirection="SortDirection.Descending" /> </DataGrid>
@code{ [Inject] public EmployeeData EmployeeData { get; set; } private List<Employee> employeeList; private Employee selectedEmployee; protected override async Task OnInitializedAsync() { employeeList = await EmployeeData.GetDataAsync(); await base.OnInitializedAsync(); } }
ApplySorting
You can use the ApplySorting
method to programmatically specify the columns to sort by.
In this example, there are two buttons to change the sort order programmatically.
<Button Color="Color.Secondary" Clicked="OnResetClicked">Reset sorting</Button> <Button Color="Color.Primary" Clicked="OnPredefinedClicked">Apply predefined sorting</Button> <DataGrid @ref="dataGrid" TItem="Employee" Data="" Responsive Sortable SortMode="DataGridSortMode.Multiple" ShowPager="true" > <DataGridCommandColumn /> <DataGridColumn Field="@nameof(Employee.Id)" Caption="#" Sortable="false" /> <DataGridColumn Field="@nameof(Employee.FirstName)" Caption="First Name" /> <DataGridColumn Field="@nameof(Employee.LastName)" Caption="Last Name" /> <DataGridColumn Field="@nameof(Employee.Email)" Caption="Email" /> <DataGridColumn Field="@nameof(Employee.Salary)" Caption="Salary" /> <DataGridNumericColumn TItem="Employee" Field="@nameof( Employee.Childrens )" Caption="Childrens"/> <DataGridColumn Field="@nameof(Employee.Gender)" Caption="Gender" /> </DataGrid>
@code{ [Inject] public EmployeeData EmployeeData { get; set; } private List<Employee> employeeList; private DataGrid<Employee> dataGrid; protected override async Task OnInitializedAsync() { employeeList = await EmployeeData.GetDataAsync(); await base.OnInitializedAsync(); } private Task OnResetClicked() => dataGrid.ApplySorting(Array.Empty<DataGridSortColumnInfo>()); private Task OnPredefinedClicked() => dataGrid.ApplySorting( new DataGridSortColumnInfo(nameof(Employee.Childrens), SortDirection.Descending), new DataGridSortColumnInfo(nameof(Employee.Gender), SortDirection.Ascending) ); }
API
See the documentation below for a complete reference to all of the props and classes available to the components mentioned here.