How to show columns wise sum of all int or decimal value which are in DB Table

149 views Asked by At

i'm new to blazor and using syncfussion datagrid to show data coming from database. Now there are multiple types of data like string int or decimal. What i want to do is that i want to show columns wise sum of those which have int or decimal type and it has to be dynamically like if i'll add another int or decimal property it has to be going to show sum of that column as well. On each column footer there is sum of all the values of specific columns.

I'm attaching a image as a example enter image description here

Please see refference image what i exactly want. In picture it showing sum of debit and credit but in my case there are multiple int or decimal values so i have to show it dynamically.

1

There are 1 answers

2
dani herrera On BEST ANSWER

Simple way

You can use a foreach loop to add aggregates dynamically:

@page "/fetchdata"
@using gridagg.Data
@inject WeatherForecastService ForecastService

<SfGrid DataSource="@forecasts">
    <GridAggregates>
        @foreach (var p in GetPropertyNames())
        {
            <GridAggregate>
                <GridAggregateColumns>
                    <GridAggregateColumn Field=@p Type="AggregateType.Sum">
                        <FooterTemplate>
                            @{
                                var aggregate = (context as AggregateTemplateContext);
                                <div>
                                    <p>Sum: @aggregate?.Sum</p>
                                </div>
                            }
                        </FooterTemplate>
                    </GridAggregateColumn>
                </GridAggregateColumns>
            </GridAggregate>
        }
    </GridAggregates>
</SfGrid>

In same file:

@code {
    private WeatherForecast[]? forecasts;
    protected override async Task OnInitializedAsync()
    {
        forecasts =
           await ForecastService
           .GetForecastAsync(DateOnly.FromDateTime(DateTime.Now));
    }
    private string[] GetPropertyNames()
        =>
        typeof(WeatherForecast)
        .GetProperties()
        .Where(p => p.PropertyType == typeof(int)) // or decimal
        .Select(p => p.Name)
        .ToArray();
}

Using RenderFragment

You may consider developing a custom component for the aggregation segment. Here's a sample to serve as a starting point for your work:

@page "/fetchdata"
@using gridagg.Data
@inject WeatherForecastService ForecastService

<SfGrid DataSource="@forecasts">
    @CustomAgg.GetAggregates(forecasts)
</SfGrid>

This is a new file named CustomAgg.razor:

@code {
    public static RenderFragment GetAggregates<T>(IEnumerable<T>? _)
    {
        var properties =
            typeof(T)
            .GetProperties()
            .Where(p => p.PropertyType == typeof(int)) // or decimal
            .Select(p => p.Name)
            .ToArray();

        return
            @<GridAggregates>
                @foreach (var p in properties)
                {
                    <GridAggregate>
                        <GridAggregateColumns>
                            <GridAggregateColumn Field=@p Type="AggregateType.Sum">
                                <FooterTemplate>
                                    @{
                                var aggregate = (context as AggregateTemplateContext);
                                        <div>
                                            <p>Sum: @aggregate?.Sum</p>
                                        </div>
                                    }
                                </FooterTemplate>
                            </GridAggregateColumn>
                        </GridAggregateColumns>
                    </GridAggregate>
                }
            </GridAggregates>;
    }
}