This is a migrated thread and some comments may be shown as answers.

How to implement OnCustomAggregate

1 Answer 188 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Jonas
Top achievements
Rank 1
Jonas asked on 18 Jan 2018, 12:20 PM

I have a RadGrid in an ASP page. The grid is grouped by a column containing organization names. There is also a column containing prices and I want to sum them up in the group footer (and in the grid footer as well). So far so good. I can add Aggregate="Custom" to the column I want to sum.

There is also a column containing checkboxes. I want to exclude the rows where the checkbox is checked. So instead I add Aggregate="Custom" to the column and OnCustomAggregate="rg_CustomAggregate" on the grid. Now I need to implement this method rg_CustomAggregate but I'm struggling with how to actually browse through the rows in order to sum the price in the rows with the unchecked checkboxes.

The base for the method looks like this so far:

protected void rg_CustomAggregate(object sender, GridCustomAggregateEventArgs e)
{
  int sum = 0;
  if (e.Item is GridGroupFooterItem)
  {
    // TODO: The magic.
    e.Result = sum;
  }
  if (e.Item is GridFooterItem)
  {
    // TODO: Som other magic.
    e.Result = sum;
  }
}

Any tips on how the magic should be implemented is gladly accepted. I have had a hard time finding examples of this on the web.

1 Answer, 1 is accepted

Sort by
0
Jonas
Top achievements
Rank 1
answered on 18 Jan 2018, 02:48 PM

After downloading the zip file mentioned (customaggreagtes.zip) on this page https://www.telerik.com/forums/real-customaggregate-group-footer-example-needed I managed to com up with the following solution:

 

protected void rg_CustomAggregate(object sender, GridCustomAggregateEventArgs e)
{
  if (e.Item is GridGroupFooterItem)
  {
    GridGroupFooterItem gridFooterItem = e.Item as GridGroupFooterItem;
    GridItem[] groupHeaders = rg.MasterTableView.GetItems(GridItemType.GroupHeader);
    foreach (GridGroupHeaderItem groupHeader in groupHeaders)
    {
      if (groupHeader.GroupIndex == gridFooterItem.GroupIndex)
      {
        decimal groupSum = 0;
        foreach (GridItem childItem in groupHeader.GetChildItems())
        {
          FunctionSummaryDTO functionSummary = (FunctionSummaryDTO)childItem.DataItem;
          if (functionSummary.UpfrontPayment == false)
          {
            groupSum += functionSummary.InvoiceAmount;
          }
        }
        e.Result = groupSum;
      }
    }
  }
  if (e.Item is GridFooterItem)
  {
    decimal totalSum = 0;
    GridItem[] groupFooters = rg.MasterTableView.GetItems(GridItemType.GroupFooter);
    foreach (GridGroupFooterItem groupFooter in groupFooters)
    {
      decimal parseBuffer = 0;
      decimal.TryParse(groupFooter["colInvoiceAmount"].Text, out parseBuffer);
      totalSum += parseBuffer;
    }
    e.Result = totalSum;
  }
}

 

This is probably not the most elegant solution but it does the trick. It's taken a bot out of context so I will try to explain som details in order for it to make a little bit more sense.

  • FunctionSummaryDTO is a class that only contains properties. It contains the data of one row in the grid.
  • functionSummary.UpfrontPayment is the data for the checkbox.
  • functionSummary.InvoiceAmount is the price that should be summed.
  • colInvoiceAmount is the unique name of the collumn containing the price to be summed.

 

Tags
Grid
Asked by
Jonas
Top achievements
Rank 1
Answers by
Jonas
Top achievements
Rank 1
Share this question
or