We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Custom Average

hi

How to average for cells value greater than zero?

Meanwhile, I don't want from Queryable use


Example :

Row1=10

Row2= 20

Row3= 0

Row4= 0

average=15


2 Replies

MS Monisha Saravanan Syncfusion Team October 24, 2022 08:43 AM

Hi Ali,


Greetings from Syncfusion support.


Query: “How to average for cells value greater than zero?”


We have checked your query and we would like to inform that we can perform our own aggregate calculation by using custom methods. Kindly check the below code snippet for your reference. Here we have calculated the aggregates in custom methods and returned the values in the aggregate footer template.


 

<SfGrid DataSource="@Orders" AllowPaging="true">

    <GridPageSettings PageSize="8"></GridPageSettings>

    <GridAggregates>

 

        <GridAggregate>

            <GridAggregateColumns>

                <GridAggregateColumn Field=@nameof(Order.Freight) Type="AggregateType.Average" Format="C2">

                    <FooterTemplate>

                        @{

                            var aggregate = CalculateAgg();

                         

                            @aggregate;

 

                        }

                    </FooterTemplate>

                </GridAggregateColumn>

            </GridAggregateColumns>

        </GridAggregate>

    </GridAggregates>

   

</SfGrid>

 

@code{

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

  

    public double CalculateAgg()

    {var sum = Orders.Sum(x => x.Freight);

        var val = Orders.Where(c=> c.Freight != 0).ToList();

        double data = (double) sum/val.Count;

        return data;

    }

 

}


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


Please let us know if you have any concerns.


Regards,

Monisha



AL ali October 24, 2022 11:27 AM

thank you

 When we make aggregate columns by loop,it throw an exception:

 System.ArgumentException: An item with the same key has already been added. Key: S9.Pressure.Value

Is there a way to check if the key exists in the collection?

Or can we make a separate component for aggregate column?

 <GridAggregates>

                    <GridAggregate>

                        <GridAggregateColumns>


                                @if (Aggerate != null)

                                {

                               

                                    foreach (var col in Aggerate.Columns)

                                    {

                                     var pressure = "S" + col.Key.ToString() + ".Pressure.Value";

                                     <GridAggregateColumn Field="@pressure" Type="AggregateType.Average">

                                    </GridAggregateColumn>

                                   }

                                }

                            }

                        </GridAggregateColumns>

                    </GridAggregate>

                </GridAggregates>




Loader.
Live Chat Icon For mobile
Up arrow icon