radzenhq / radzen-blazor

Radzen Blazor is a set of 90+ free native Blazor UI components packed with DataGrid, Scheduler, Charts and robust theming including Material design and FluentUI.
https://www.radzen.com
MIT License
3.57k stars 798 forks source link

Initial Sort on RadzenDataGridColumn with SortProperty Set Not Working #752

Closed hereiznoy closed 1 year ago

hereiznoy commented 1 year ago

When setting both the SortOrder and SortProperty properties of a RadzenDataGridColumn, on initial load the grid is not sorted by this column. However subsequent order operations (by clicking the sorting button on the column's UI) correctly orders records ascending and descending by that column. Also, removing the SortProperty from the column declaration results in correct application of sort behavior on grid load (although in my case it's now ordering by the incorrect value).

I've noticed by inspecting the LoadDataArgs object in the grid's LoadData event that on initial load the LoadDataArgs.OrderBy property is empty.

Example (SortValue is a property on the ActivityViewModel but is not a column displayed in the grid):


<RadzenDataGridColumn
    TItem="ActivityViewModel"
    Property="GGRCNumber"
    Title="GGRC Number"
    SortOrder="SortOrder.Ascending"
    SortProperty="SortValue"
    Width="150px">
    ...
</RadzenDataGridColumn>

OS: Windows 10
Browser: Microsoft Edge v110.0.1543.0 dev
enchev commented 1 year ago

I'm afraid that I'm unable to reproduce such problem on our demo: https://blazor.radzen.com/datagrid-loaddata image

hereiznoy commented 1 year ago

@enchev In your example, did you set the SortProperty property of RadzenDataGridColumn? It doesn't look like it, but I can't tell for sure from your screenshot. But it's the combination of that property plus the SortOrder that I'm seeing an issue with. I can reproduce this in a very basic example (see below) with no dependencies other than the Radzen data grid stuff.

Perhaps I'm using SortProperty incorrectly. What I want is to sort a given grid column by a value other than the one being displayed. Which works fine except on initial load.

Example (I believe this should initially order second record (with NameSortValue = x first, but it does not)) :

<RadzenDataGrid
    TItem="Thing"
    Data="_things"
    AllowSorting="true">
    <Columns>
        <RadzenDataGridColumn
            TItem="Thing"
            Property="Id"
            Title="Id">
        </RadzenDataGridColumn>
        <RadzenDataGridColumn
            TItem="Thing"
            Property="Name"
            Title="Name"
            SortProperty="NameSortValue"
            SortOrder="SortOrder.Ascending">
        </RadzenDataGridColumn>
    </Columns>
</RadzenDataGrid>

@code {

    public class Thing
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string NameSortValue { get; set; }
    }

    private readonly IEnumerable<Thing> _things = new[]
    {
        new Thing {Id = 1, Name = "Bart", NameSortValue = "y"},
        new Thing {Id = 2, Name = "Jason", NameSortValue = "x"}
    };
}
enchev commented 1 year ago

Fixed in https://github.com/radzenhq/radzen-blazor/commit/7aa37cd6cb2e5ddaf1876b82b09927f18a3794fe