Repository navigation
[DataGrid] Add DefaultSortPriority and leave the grid unsorted when the last sort level is removed - #5460
Conversation
… sort - Add DefaultSortPriority to ColumnBase. Columns declaring IsDefaultSortColumn sort by priority (lower first); columns without one follow in declaration order, so grids without priorities behave as before - Apply the declared sort once the columns are collected, so the first data request already carries it in priority order - In single sort mode, the declared column that sorts first (lowest DefaultSortPriority) wins - In single sort mode, RemoveSortByColumnAsync only keeps the column the grid is sorted by by default; removing the sort of any other column declaring IsDefaultSortColumn restores the default - ResetSortAsync, Shift+S and removing the last sort level restore the sort in priority order - Document DefaultSortPriority on the multi-column sorting page
…moved In multiple sort mode, removing the last sort level (right-click on a header, Clear in the header menu or RemoveSortByColumnAsync) brought the declared default sort back, so sorts reappeared right after the user removed them. - Removing the last level now leaves the grid unsorted, as ClearSortAsync does, and saves it as unsorted in the URL. ResetSortAsync, Reset and Shift+S still return to the declared sort - The header menu offers Reset whenever some column declares a sort and the grid is not sorted that way, including with one level or none, since Clear no longer goes back to the declared sort - Single sort mode is unchanged - Declare a second default sort column in the multi-sort demo and add a Reset sort button
There was a problem hiding this comment.
🟡 Changes recommended
Shift+S cannot restore the declared sort after the final sort level is removed.
1 open finding
What changed in this PR
This follow-up to #5312 adds explicit default-sort priorities to DataGrid and separates clearing sorts from restoring defaults.
Changes:
- Adds
DefaultSortPriorityfor declared sort ordering. - Leaves the grid unsorted when its final sort level is removed.
- Updates reset-menu visibility, regression tests, documentation, and demo controls.
| File | Description |
|---|---|
| tests/Core/Components/DataGrid/FluentDataGridMultiSortTests.razor | Tests priority ordering, clearing, and reset behavior. |
| src/Core/Components/DataGrid/FluentDataGrid.razor.cs | Implements ordered defaults and revised sort removal. |
| src/Core/Components/DataGrid/Columns/ColumnSortMenuItems.razor | Offers Reset when sorting differs from defaults. |
| src/Core/Components/DataGrid/Columns/ColumnBase.razor.cs | Adds the documented priority parameter. |
| examples/Demo/FluentUI.Demo.Client/Documentation/Components/DataGrid/Pages/DataGridMultiSortPage.md | Documents priorities and clear/reset semantics. |
| examples/Demo/FluentUI.Demo.Client/Documentation/Components/DataGrid/Examples/DataGridMultiSort.razor | Demonstrates multiple defaults and a reset button. |
🧠 Review effort: Balanced
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
…t level is removed
|
We've added a new requirement regarding PRs
|
|
Vincent Baaij (@vnbaaij) Have we received a request to add these new features? |
|
No, we didn't get any requests for adding this. In my opinion this is typically a feature that we need to have a discussion about before coding starts. The DataGrid is already a quity complex component as it is now. And I think it is OK if doesn't have all the bells and whistles. |
I agree. Steven Rasmussen (@StevenRasmussen) Could you start a discussion thread to tell us more about your motivation and get feedback from the community? In the meantime, I'm closing this PR, which can be reactivated later if necessary. |
I was under the impression that by me taking the effort to build this feature, open the PR, and go through the review process, that I was "requesting" this feature. Also, I am a bit confused by this response. Why would you wait until the very end, when this PR has been reviewed multiple times, and is ready to be merged, to bring this up? Why waste my time and your time only to say "we should have a discussion about this first"? I would think that if you had any reservations about this PR that that would be the first comment. Anyway, I will do as you request and open a discussion to lay out the case/need for this feature. |
|
Discussion here: #5497 |
|
I'm sorry. We really should have responded earlier. In any case, if the request is approved, this PR will simply be reopened. |

Pull Request
📖 Description
Follow-up to the multi-column sorting work in #5312.
Add
DefaultSortPriorityto order the declared default sortWhen more than one column declares
IsDefaultSortColumn, the default sort follows the order the columns are declared in. This forces the most important sort column to also be the first column, which often isn't what the layout needs. It also gets in the way of grids that build their columns and default sort at runtime, for example from a saved view.ColumnBase.DefaultSortPriority(int?, defaultnull) sets that order:IsDefaultSortColumn.ItemsorItemsProvider) already carries it. The grid doesn't load unsorted and then sort again.ResetSortAsync, Shift+S and the header menu's Reset restore the sort in priority order.SaveStateInUrl) still takes precedence over the declared sort.Fix: removing the last sort level brought the default sort back
In multiple sort mode, removing the last remaining sort level restored the declared default sort. This happened with right-click on a header, Clear in the header menu and
RemoveSortByColumnAsync. A user removing sorts one by one saw them reappear, and Clear did the opposite of what its label says. It only shows with a declared default sort, which the demo didn't have.ClearSortAsyncdo. WithSaveStateInUrlit is saved as unsorted (orderby=), so a reload doesn't bring the default sort back.ResetSortAsync, Shift+S and Reset still return to the default sort.Behavior changes for existing grids
RemoveSortByColumnAsynconly keeps the sort of the column the grid is actually sorted by by default. Before, any column markedIsDefaultSortColumnwas kept, even one that wasn't the default in single mode, so removing its sort did nothing.👩💻 Reviewer Notes
FluentDataGrid.RebuildDeclaredSort. It only sorts when a column sets a priority, so grids without priorities skip that work on every render. The declared sort is applied inFinishCollectingColumns, before the URL state is applied.RemoveSortByColumnAsync. The Reset visibility uses the new internalIsSortedAsDeclared.Smoke test: run the demo and open
/DataGrid/MultiSort. The example now declares two default sort columns (Department, then Start Date newest first) and has a Reset sort button.📑 Test Plan
New tests in
FluentDataGridMultiSortTests.razor:DefaultSortPriority:aria-sort.int.MaxValuevalues.IsDefaultSortColumnis ignored.RemoveSortByColumnAsynconly keeps the column the grid is actually sorted by by default.The full unit test suite passes, and the Release build (including net11.0 analyzers) has no warnings.
✅ Checklist
General
Issue discussion declaration
Discuss changes before implementing them to confirm that the problem and proposed approach fit the project. See Discuss before implementing for the process and exceptions.
AI contribution declaration
AI assistance is allowed; fully AI-generated or autonomous pull requests without meaningful human review are not accepted. See the AI contribution policy.
Component-specific