New to Telerik UI for ASP.NET AJAXStart a free 30-day trial

Performing Calculations in Group Header

Although you can add aggregations to group expressions so that the group headers in RadGrid display aggregated values, there may be times when you want to display other calculated values that are not included in the standard aggregations. You can use the ItemDataBound event to calculate your own values and add them to the group headers.

The following example illustrates how this is done. It uses the "Sum" and "Count" aggregated values to calculate an average and adds this calculated value to the DataCell.Text property of the grid header item:

ASP.NET
<telerik:RadGrid RenderMode="Lightweight" ID="RadGrid1" runat="server" DataSourceID="SqlDataSource1" Width="100%"
  PageSize="10" AllowSorting="True" AllowPaging="True" ShowGroupPanel="True">
  <MasterTableView>
    <GroupByExpressions>
      <telerik:GridGroupByExpression>
        <SelectFields>
          <telerik:GridGroupByField FieldAlias="Country" FieldName="Country" />
          <telerik:GridGroupByField FieldAlias="Total" Aggregate="Sum" FieldName="TotalAmount" />
          <telerik:GridGroupByField FieldAlias="Count" Aggregate="Count" FieldName="CustomerID" />
        </SelectFields>
        <GroupByFields>
          <telerik:GridGroupByField FieldAlias="Country" FieldName="Country" />
        </GroupByFields>
      </telerik:GridGroupByExpression>
    </GroupByExpressions>
  </MasterTableView>
  <ClientSettings>
    <Resizing AllowColumnResize="True"></Resizing>
  </ClientSettings>
  <PagerStyle Mode="NumericPages" />
</telerik:RadGrid>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
          SelectCommand="SELECT Customers.CustomerID, Country, City, CompanyName, ContactName, Count(Orders.OrderID) as TotalOrders,
                         Sum(OD.Quantity * OD.UnitPrice - Discount) as TotalAmount
                         FROM Customers, Orders, [Order Details] OD
                         WHERE (Orders.CustomerID = Customers.CustomerID) and
                         (OD.OrderID = Orders.OrderID)
                         Group By Customers.CustomerID, Country, City, CompanyName, ContactName">
</asp:SqlDataSource>
Not finding the help you need?
Contact Support