How to exclude one row from aggregation?

1 Answer 145 Views
Grid
David
Top achievements
Rank 1
Iron
Iron
Veteran
David asked on 24 Oct 2023, 07:27 PM

I am using Aggregate="sum", but one of my rows contains years and should not be aggregated.

Is there a way?

1 Answer, 1 is accepted

Sort by
0
Vasko
Telerik team
answered on 27 Oct 2023, 12:48 PM

Hello David,

To achieve the desired behavior, you can use Custom Aggregate functionality.

For example, you can change the Aggregate property to "Custom", instead of "Sum", because in the given scenario you'd want to exclude the date from the end result. I've prepared a sample code snippet that demonstrates the outcome of this approach:  

  • The DataType needs to be a string so the custom aggregation works. The markup of the Grid used in this example is the following: 
    <telerik:RadGrid
        ID="RadGrid1"
        runat="server"
        AllowPaging="True"
        Width="800px"
        OnNeedDataSource="RadGrid1_NeedDataSource"
        ShowFooter="true">
        <MasterTableView AutoGenerateColumns="False">
            <Columns>
                <telerik:GridBoundColumn DataField="MyField" DataType="System.String" Aggregate="Custom">
                </telerik:GridBoundColumn>
            </Columns>
        </MasterTableView>
    </telerik:RadGrid>
  • In the backend part of the code, the Grid is bound to some sample data: 
        private DataTable OrdersTable() // Creating a collumn with rows of numeric and DateTime values
        {
            DataTable dt = new DataTable();
    
            dt.Columns.Add(new DataColumn("MyField", typeof(string)));
    
            for (int i = 0; i < 5; i++)
            {
                int index = i + 1;
    
                DataRow row = dt.NewRow();
    
                if (index == 2 || index == 4)
                {
                    row["MyField"] = DateTime.Now.Date.ToString();
                }
                else
                {
                    row["MyField"] = index.ToString();
                }
    
                dt.Rows.Add(row);
            }
    
            return dt; // This will return a test grid with one column with 5 rows, 2 of which are dates
        }
    
        protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
        {
            RadGrid grid = sender as RadGrid;
            DataTable sourceData = OrdersTable();
            grid.DataSource = sourceData;
    
            double sum = 0;
    
            foreach (DataRow row in sourceData.Rows)
            {
                double numericValue;
                 
                if (double.TryParse(row["MyField"].ToString(), out numericValue)) // If the value in the row is a number, add it to the aggregated sum.
                {
                    sum += Convert.ToDouble(row["MyField"]);
                }
            }
    
            grid.MasterTableView.GetColumn("MyField").FooterText = "Sum: " + sum.ToString();
        }

With the above code, we achieve the following:

  • After setting the data source for the RadGrid, we loop through each data row and check if its value is of type double (or if it's a numeric value) using double.TryParse().
  • We add the value to our aggregate sum if it is a number.
  • Finally, we set the FooterText of the column to display our custom aggregate.

For more information about aggregation, I suggest taking a look at the following articles: 

Please give this method a try and let me know if it is helpful.

Kind regards,
Vasko
Progress Telerik

Stay tuned by visiting our public roadmap and feedback portal pages! Or perhaps, if you are new to our Telerik family, check out our getting started resources
David
Top achievements
Rank 1
Iron
Iron
Veteran
commented on 27 Oct 2023, 01:28 PM

I tried and it does not seem to work with GridNumericColumn.

Does it have to be GridBoundColumn?

Thank you for your help

David
Top achievements
Rank 1
Iron
Iron
Veteran
commented on 27 Oct 2023, 01:30 PM

Never mind, i figured it out.

Thx again!

Tags
Grid
Asked by
David
Top achievements
Rank 1
Iron
Iron
Veteran
Answers by
Vasko
Telerik team
Share this question
or