Aggregate a column based on the values of another column that you do not want to show in a grid

good morning, in a table i have two fields. one expresses a numeric value and the other the corresponding type (string) . i don't know if you play golf or know the sport.

basically let's say for column 1 the values only 4,5,6 etc and for column two the corresponding result “Birdie, Par, bogey etc.”

I would like to have in the footer the numerical average of the results but without showing the numeric column. I would need the total to be under the string type column

how can i do this ? 


thx 


1 Reply

PS Prathap Senthil Syncfusion Team August 19, 2024 02:40 PM UTC

Hi Francesco Pruneri,

Based on your query, to calculate the numerical average of the results without displaying the numeric column under the string type column, we suggest using the CustomAggregate feature to achieve this. Kindly refer to the code snippet and sample below for your reference.

<SfGrid DataSource="@Orders">

 

    <GridAggregates>

        <GridAggregate>

            <GridAggregateColumns>

                <GridAggregateColumn Field=@nameof(OrderData.CustomerID) Type="Syncfusion.Blazor.Grids.AggregateType.Custom">

                    <FooterTemplate>

                        @{

                            <div>

                                <p>Freight Average: @CalculateAverageFreight()</p>

                            </div>

                        }

 

                    </FooterTemplate>

                </GridAggregateColumn>

            </GridAggregateColumns>

        </GridAggregate>

    </GridAggregates>

    <GridColumns>

        <GridColumn Field=@nameof(OrderData.OrderID) HeaderText="Order ID" TextAlign="Syncfusion.Blazor.Grids.TextAlign.Right" Width="120"></GridColumn>

        <GridColumn Field=@nameof(OrderData.Freight) HeaderText="Freight"  Visible="false" TextAlign="Syncfusion.Blazor.Grids.TextAlign.Right"></GridColumn>

        <GridColumn Field=@nameof(OrderData.CustomerID) HeaderText="Customer ID" Width="150"></GridColumn>

        <GridColumn Field=@nameof(OrderData.OrderDate) HeaderText="Order Date" Format="d" Type="Syncfusion.Blazor.Grids.ColumnType.Date" TextAlign="Syncfusion.Blazor.Grids.TextAlign.Right" Width="150"></GridColumn>

 

        <GridColumn Field=@nameof(OrderData.ShipCountry) HeaderText="Ship Country" Width="150"></GridColumn>

    </GridColumns>

</SfGrid>

 

@code {

 

    public List<OrderData> Orders { get; set; }

 

    protected override void OnInitialized()

    {

        Orders = OrderData.GetAllRecords();

    }

 

    private double CalculateAverageFreight()

    {

        if (Orders == null || Orders.Count == 0)

        {

            return 0;

        }

 

        return Orders.Average(x => x.Freight);

    }

}


Reference: https://blazor.syncfusion.com/documentation/datagrid/custom-aggregate

Sample: https://blazorplayground.syncfusion.com/embed/VNrpZFiaSrgwrpKi?appbar=true&editor=true&result=true&errorlist=true&theme=bootstrap5


Regards,
Prathap Senthil


Loader.
Up arrow icon