Sorting a datagrid where the property to be sorted is a dictionary item?

Hi, I have a data grid in which some columns in which the value displayed is determined at runtime, as it is fetched from a dictionary based on what "categories" are the most common. I would like these columns to be sortable, however it appears sorting can be done on a property only - whereas I would like to provide a lambda that fetches the value.

I'm wondering if there's a reasonable workaround, or some sort of SortFunction parameter could be added?

Since I do have an upper bound on the amount of columns, it would be possible to replace ProductCounts in my example data item with properties like Product1Count, Product2Count, etc, then set these values based on which products will be shown. However it would be nice to not have to do this.

Example code:

@page "/testpage"

<RadzenDataGrid AllowSorting="true" Data=@Trucks>
        <RadzenDataGridColumn TItem=TruckCargo Property="Name"/>

        @foreach (var product in productNames)
            <RadzenDataGridColumn TItem=TruckCargo Title=@product> @* SortProperty = ? *@
                <Template Context="datum">
                    @(datum.ProductCounts.ContainsKey(product) ? datum.ProductCounts[product] : 0)

    private List<TruckCargo> Trucks = new()
        new("Truck 1", new() {{"Water", 2}, {"Wood", 1} }),
        new("Truck 2", new() {{"Metal", 5}, {"Wood", 3} }),

    private List<string> productNames = new() { "Water", "Wood", "Metal" };

    private class TruckCargo
        public string Name { get; set; } = "";
        public Dictionary<string, int> ProductCounts { get; set; } = new();

        public TruckCargo(string name, Dictionary<string, int> productCounts)
            Name = name;
            ProductCounts = productCounts;
1 Like

Thank you, not sure why I didn't see that before