简体   繁体   English

如何实现 OnCustomAggregate (Telerik:RadGrid)

[英]How to implement OnCustomAggregate (Telerik:RadGrid)

I have a RadGrid in an ASP page.我在 ASP 页面中有一个 RadGrid。 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.我可以将Aggregate="Custom"添加到要求和的列中。

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 RadGrid.因此,我将Aggregate="Custom"添加到OnCustomAggregate="rg_CustomAggregate"的列和OnCustomAggregate="rg_CustomAggregate" 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.现在我需要实现这个方法 rg_CustomAggregate 但我正在努力解决如何实际浏览行以便对带有未选中复选框的行中的价格求和。

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.我很难在网上找到这样的例子。

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:下载本页https://www.telerik.com/forums/real-customaggregate-group-footer-example-needed上提到的 zip 文件 (customaggregate.zip) 后,我设法提出了以下解决方案:

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. FunctionSummaryDTO是一个只包含属性的类。 It contains the data of one row in the grid.它包含网格中一行的数据。
  • functionSummary.UpfrontPayment is the data for the checkbox. functionSummary.UpfrontPayment是复选框的数据。
  • functionSummary.InvoiceAmount is the price that should be summed. functionSummary.InvoiceAmount是应该求和的价格。
  • colInvoiceAmount is the unique name of the column containing the price to be summed. colInvoiceAmount是包含要汇总的价格的列的唯一名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM