MudDataGrid: Sorting causes Unhandled exception

See original GitHub issue

Bug type

Component

Component name

MudDataGrid

What happened?

Sorting causes Unhandled exception at var removedSortDefinitions = new HashSet<string>(SortDefinitions.Keys);

It says SortDefinitions are managed by the data grid automatically when using the built-in filter UI.

So is this a bug, or in case of using TemplateColumn I need to specify SortDefinitions?

It was working fine in the previous version with the Column component, I was doing its job without me specifying any sorting mechanisms.

Unhandled exception rendering component: Value cannot be null. (Parameter 'key')
System.ArgumentNullException: Value cannot be null. (Parameter 'key')
   at System.Collections.Generic.Dictionary`2.TryInsert(TKey key, TValue value, InsertionBehavior behavior)
   at System.Collections.Generic.Dictionary`2.set_Item(TKey key, TValue value)
   at MudBlazor.MudDataGrid`1.SetSortAsync(String field, SortDirection direction, Func`2 sortFunc)
   at MudBlazor.HeaderCell`1.SortChangedAsync(MouseEventArgs args)

Expected behavior

As in the previous version, it shouldn’t cause an exception when trying to sort items and could do it in some default behavior.

Reproduction link

https://try.mudblazor.com/snippet/GaQnaRGwIaHRPlKU

Reproduction steps

  1. Specify columns in datagrid with TemplateColumn component
  2. Try to sort any sortable column

Relevant log output

No response

Version (bug)

6.2.0

Version (working)

6.1.9

What browsers are you seeing the problem on?

Firefox, Chrome, Microsoft Edge

On what operating system are you experiencing the issue?

Windows

Pull Request

  • I would like to do a Pull Request

Code of Conduct

  • I agree to follow this project’s Code of Conduct

Issue Analytics

  • State:closed
  • Created 6 months ago
  • Comments:21 (12 by maintainers)

github_iconTop GitHub Comments

2reactions
tjsciencecommented, Apr 9, 2023

I am aware of several instances where developers are attempting to do this with datagrd. I will work on a best practice example.

2reactions
wichurcommented, Apr 6, 2023

Not a solution but temporary fix.

  1. Create class to act as TemplateColumn but inheriting from PropertyColumn
using MudBlazor;

namespace Something;

public class TestColumn<T, TProperty> : PropertyColumn<T, TProperty>
{
    protected override object? CellContent(T item) => (object)null;

    protected override object? PropertyFunc(T item) => (object)null;

    protected override void SetProperty(object item, object value)
    {
    }
}
  1. Now we have Property exposed so its name will be passed to SetSortAsync and can be used it like this:
<TestColumn Property="x => x.PupilName"
                    SortBy="x => x.PupilName"
                        Title="ShouldNotSeeIt"
                        TProperty="string"
                        T="ListItem">
          <HeaderTemplate>
              ShouldSeeIt
          </HeaderTemplate>
          <CellTemplate>
              <MudHighlighter HighlightedText="@this.list?.SearchString"
                              Text="@context.Item?.PupilName" />
          </CellTemplate>
</TestColumn>
  1. Template, sorting works obraz

Hope it helps until fix is out.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Data Grid
A sortable, filterable data grid with multiselection and pagination. ... The <MudDataGrid> is used to display and work with small amounts of data...
Read more >
c# - DataGridView column sort exception
There are two possible causes of this issue, either you have not bound your DataGridView or you are using an incorrect column name....
Read more >
DataGrid sorting and filtering stopped working
Hello,. All of the actions in DataGrid suddenly stopped working. ... Unhandled exception rendering component: Object reference not set to an ...
Read more >
Unhandled exception rendering component: Value cannot ...
Hi I have created a Blazor WA app and I have added a DxDataGrid component. Everything works fine: the grid is correctly populated...
Read more >
Stack Overflow while sorting a grid in UI for WinForms
No matter what I try I am getting a stackoverflow error on the CellValueNeeded event anytime I try to sort any of my...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found