DataGrid Sort on Nullable Property

I have a column that's bound to a property, that is a class, that may be null and when I sort on that column if throws a null exception.

Item.ClassProperty.Id
<RadzenDataGridColumn TItem="Shipment" Property="Item.ClassProperty.Id" />

Where ClassProperty could be null.

In theory, I feel like null values should just float to the top/bottom instead of throwing an exception. Is there anyway to allow nulls or am I going to have to create a sort property for each of my nullable paths on the Item itself?

Can you post the stack trace?

Does this help?

crit: Microsoft.AspNetCore.Components.WebAssembly.Rendering.WebAssemblyRenderer[100]
      Unhandled exception rendering component: Arg_NullReferenceException
System.NullReferenceException: Arg_NullReferenceException
   at System.Linq.EnumerableSorter`2[[Olis.Server.Core.Shipments.Shipment, Olis.Server.Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null],[System.String, System.Private.CoreLib, Version=5.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].ComputeKeys(Shipment[] elements, Int32 count)
   at System.Linq.EnumerableSorter`1[[Olis.Server.Core.Shipments.Shipment, Olis.Server.Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].ComputeMap(Shipment[] elements, Int32 count)
   at System.Linq.EnumerableSorter`1[[Olis.Server.Core.Shipments.Shipment, Olis.Server.Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].Sort(Shipment[] elements, Int32 count)
   at System.Linq.OrderedEnumerable`1[[Olis.Server.Core.Shipments.Shipment, Olis.Server.Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].SortedMap(Buffer`1 buffer)
   at System.Linq.OrderedEnumerable`1.<GetEnumerator>d__17[[Olis.Server.Core.Shipments.Shipment, Olis.Server.Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].MoveNext()
   at System.Collections.Generic.List`1[[Olis.Server.Core.Shipments.Shipment, Olis.Server.Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]..ctor(IEnumerable`1 collection)
   at System.Linq.Enumerable.ToList[Shipment](IEnumerable`1 source)
   at Radzen.PagedDataBoundComponent`1[[Olis.Server.Core.Shipments.Shipment, Olis.Server.Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].get_PagedView()
   at Radzen.Blazor.RadzenDataGrid`1[[Olis.Server.Core.Shipments.Shipment, Olis.Server.Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].DrawGroupOrDataRows(RenderTreeBuilder builder, IList`1 visibleColumns)
   at Radzen.Blazor.RadzenDataGrid`1.<>c__DisplayClass6_0[[Olis.Server.Core.Shipments.Shipment, Olis.Server.Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].<DrawRows>b__0(RenderTreeBuilder builder)
   at Microsoft.AspNetCore.Components.Rendering.RenderTreeBuilder.AddContent(Int32 sequence, RenderFragment fragment)
   at Radzen.Blazor.RadzenDataGrid`1[[Olis.Server.Core.Shipments.Shipment, Olis.Server.Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].BuildRenderTree(RenderTreeBuilder __builder)
   at Microsoft.AspNetCore.Components.ComponentBase.<.ctor>b__6_0(RenderTreeBuilder builder)
   at Microsoft.AspNetCore.Components.Rendering.ComponentState.RenderIntoBatch(RenderBatchBuilder batchBuilder, RenderFragment renderFragment)
   at Microsoft.AspNetCore.Components.RenderTree.Renderer.RenderInExistingBatch(RenderQueueEntry renderQueueEntry)
   at Microsoft.AspNetCore.Components.RenderTree.Renderer.ProcessRenderQueue()

Just tried the following and it worked:

    <RadzenDataGrid AllowSorting="true" Data="@data" TItem="MyObject">
        <Columns>
            <RadzenDataGridColumn TItem="MyObject" Property="MySubProperty.Id" Title="Id"  />
        </Columns>
    </RadzenDataGrid>
@code {
    IEnumerable<MyObject> data;

    protected override void OnInitialized()
    {
        data = Enumerable.Range(0,3).Select(i => new MyObject(){ Id = i, MySubProperty = i == 0 ? null : new MyObject() { Id = i } }).ToList();
    }

    public class MyObject
    {
        public int Id { get; set; }
        public MyObject MySubProperty { get; set; }
    }
}

UPDATE: I was able to reproduce the exception with two levels of nesting - we will do our best to find a way to handle this in our next update:

The fix will be released in the next update:

1 Like