MudBlazor: MudDataGrid: Sorting causes Unhandled exception

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

About this issue

  • Original URL
  • State: closed
  • Created a year ago
  • Comments: 21 (12 by maintainers)

Most upvoted comments

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

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.

I don’t understand why they needed to break it to bring a new feature.

I apologize for any frustration caused by the breaking change. The intention was not simply to introduce a new feature, but to address the trimming issue that the previous DataGrid had. That’s why we made the decision to make a breaking change and make DataGrid more strongly typed with property expressions with less reflection.

A couple of things here:

  1. There were many other reasons to switch to expression based properties. Perhaps the most influential was being able to describe column definitions based on nested properties.
  2. There was poll early on before this change where disadvantages could be raised. The overwhelming response from the community was to go with the expression property.
  3. DataGrid before this change was marked as experimental.

All of this aside, I do realize the frustration here.

especially when you want to make it generic and can’t just pass the type parameter of the property

Can you provide a more detailed explanation of your use case where you cannot provide the property and give an better example? In your provided code snippet, I am struggling to understand why you opted for GetProperties() reflection instead of using PropertyColumn

I am not following this and would need to see an example. If you have a property, use PropertyColumn…

<PropertyColumn Property="x => x.Group" />
<PropertyColumn Property="x => x.Position" />
<PropertyColumn Property="x => x.Name" />
<PropertyColumn Property="x => x.Number" />

The only use case that comes to mind where reflection could be necessary is if the Element class is dynamic and its properties are unknown at design time.

The issue with TemplateColumn is that it is not associated with anything(no property, no name, no type etc), which is why the sort functionality does not work.

This is the whole point of TemplateColumn. It is truly for cases where you do not need or even know details about the property. If you simply want to apply sorting, but there is no specific property to speak of, you can do something like this:

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

I do actually see one issue with the current implementation. When using TemplateColumn, there should not be a sort icon showing in the header.