Wrong aggregates when grouping dynamic columns

2 Answers 11 Views
Grid
Rayko
Top achievements
Rank 1
Iron
Iron
Iron
Rayko asked on 23 Sep 2025, 03:41 PM

Hi,

I've created a Telerik Blazor grid like the one in the Repl: https://blazorrepl.telerik.com/mpONGnvz356DEAKK31

There I use dynamic columns combined with a nullable dictionary. Everything works fine.

But when I group by the bool column the Count in the group header gets mixed up.

Does anybody have an idea how to fix that?

 

Best regards,

Rayko

2 Answers, 1 is accepted

Sort by
0
Dimo
Telerik team
answered on 26 Sep 2025, 07:37 AM

Hi Rayko,

Please bind the Grid to a collection of data items rather than a collection of objects that contain both settings and nested data.

Furthermore, the support for data operations with dynamic data (especially nullable data) is unfortunately limited. Here is a simplified example that is based on yours and this KB article about binding the Grid to ExpandoObject. It shows what is currently possible.

My primary recommendation is to use strongly-typed data instead.

@using System.Dynamic

<TelerikGrid @ref="@GridRef"
             Data="@GridData"
             FilterMode="@GridFilterMode.FilterRow"
             Sortable="true"
             Groupable="true"
             Height="500px"
             TItem="@ExpandoObject"
             OnStateChanged="@OnGridStateChanged">
    <GridAggregates>
        <GridAggregate Field="CustomField1" FieldType="@typeof(string)" Aggregate="@GridAggregateType.Count" />
        <GridAggregate Field="CustomField2" FieldType="@typeof(bool)" Aggregate="@GridAggregateType.Count" />
    </GridAggregates>
    <GridColumns>
        <GridColumn Field="CustomField1" FieldType="@typeof(string)">
            <GroupHeaderTemplate>
                CustomField2 : @context.Value (Count @context.Count)
            </GroupHeaderTemplate>
            <GroupFooterTemplate>
                Count: @context.Count
            </GroupFooterTemplate>
        </GridColumn>
        <GridColumn Field="CustomField2" FieldType="@typeof(bool)">
            <GroupHeaderTemplate>
                CustomField2 : @context.Value (Count @context.Count)
            </GroupHeaderTemplate>
            <GroupFooterTemplate>
                Count: @context.Count
            </GroupFooterTemplate>
        </GridColumn>
    </GridColumns>
</TelerikGrid>

@code {
    private TelerikGrid<ExpandoObject>? GridRef { get; set; }
    private List<ExpandoObject> GridData { get; set; } = new();

    private Dictionary<string, Type> ColumnTypes = new()
    {
        { "CustomField1", typeof(string) },
        { "CustomField2", typeof(bool) }
    };

    private async Task OnGridStateChanged(GridStateEventArgs<ExpandoObject> args)
    {
        var gridState = args.GridState;

        if (gridState.GroupDescriptors.Count > 0)
        {
            foreach (var groupDescriptor in gridState.GroupDescriptors)
            {
                groupDescriptor.MemberType = ColumnTypes[groupDescriptor.Member];
            }
        }

        await GridRef!.SetStateAsync(gridState);
    }

    protected override void OnInitialized()
    {
        var random = Random.Shared;
        var sampleTexts = new[] { "Alpha", "Beta", "Gamma", "Delta", "Epsilon", "Zeta", "Eta", "Theta" };

        for (int i = 1; i <= 20; i++)
        {
            dynamic item = new ExpandoObject();

            item.CustomField1 = sampleTexts[random.Next(sampleTexts.Length)] + $" {i}";
            item.CustomField2 = random.Next(2) == 1;

            GridData.Add(item);
        }
    }
}

Regards,
Dimo
Progress Telerik

Your perspective matters! Join other professionals in the State of Designer-Developer Collaboration 2025: Workflows, Trends and AI survey to share how AI and new workflows are impacting collaboration, and be among the first to see the key findings.
Start the 2025 Survey
0
Rayko
Top achievements
Rank 1
Iron
Iron
Iron
answered on 26 Sep 2025, 02:51 PM

Hi Dimo,

Thank you for the info!

So, I think it's due to nested items. The Grid cannot calculate aggregates of them.

I built another example: https://blazorrepl.telerik.com/mfujcqPo42w2xSQS17

Is there any plan to consider nested properties for aggregates?

 

Best regards,

Rayko

Dimo
Telerik team
commented on 29 Sep 2025, 08:59 AM

Hi Rayko,

We have a feature request about built-in Grid support for aggregates of nested properties. However, the customer demand is rather low at this point.

Tags
Grid
Asked by
Rayko
Top achievements
Rank 1
Iron
Iron
Iron
Answers by
Dimo
Telerik team
Rayko
Top achievements
Rank 1
Iron
Iron
Iron
Share this question
or