RadGrid 自定 Footer
<telerik:RadGrid ID="RadGrid1" GridLines="None" runat="server" AutoGenerateColumns="false" RenderMode="Lightweight"
OnNeedDataSource="RadGrid1_NeedDataSource" OnCustomAggregate="RadGrid1_CustomAggregate"
ShowFooter="true">
<MasterTableView DataKeyNames="someColumn, moreData" ShowGroupFooter="true">
<GroupByExpressions>
<telerik:GridGroupByExpression>
<SelectFields>
<telerik:GridGroupByField FieldAlias="moreData" FieldName="moreData"
HeaderValueSeparator=" field with value: "></telerik:GridGroupByField>
</SelectFields>
<GroupByFields>
<telerik:GridGroupByField FieldName="moreData" SortOrder="None"></telerik:GridGroupByField>
</GroupByFields>
</telerik:GridGroupByExpression>
</GroupByExpressions>
<Columns>
<telerik:GridBoundColumn DataField="id" HeaderText="id" UniqueName="id" Aggregate="Sum">
</telerik:GridBoundColumn>
<telerik:GridBoundColumn DataField="firstField" HeaderText="firstField"
UniqueName="firstField">
</telerik:GridBoundColumn>
<telerik:GridBoundColumn DataField="moreData" HeaderText="moreData"
UniqueName="moreData"
Aggregate="Custom" FooterAggregateFormatString="my Decimal SUM Aggregate: {0}">
</telerik:GridBoundColumn>
<telerik:GridBoundColumn DataField="someColumn" HeaderText="someColumn"
UniqueName="someColumn"
Aggregate="Custom" FooterAggregateFormatString="my TimeSpan SUM Aggregate: {0}">
</telerik:GridBoundColumn>
</Columns>
</MasterTableView>
</telerik:RadGrid>
protected void RadGrid1_CustomAggregate(object sender, Telerik.Web.UI.GridCustomAggregateEventArgs e)
{
//get the column name it will be used for determining what to aggregate and for fetching data
string colName = e.Column.UniqueName;
//the group footer calculation is to be performed, we need only the corresponding group items data
if (e.Item is GridGroupFooterItem)
{
string msg = "group footer aggregate: ";
//get the corresponding group header as it provides child items from which we can obtain data
GridGroupHeaderItem correspondingHeader = (e.Item as GridGroupFooterItem).GroupHeaderItem;
//check column name to determine aggregation logic
if (colName == "moreData")
{
decimal counter = 0;
//get corresponding group items to iterate through and aggregate
GridItem[] groupChildItems = correspondingHeader.GetChildItems();
for (int i = 0; i < groupChildItems.Length; i++)
{
decimal currValue = (decimal)DataBinder.Eval((groupChildItems[i] as GridDataItem).DataItem, colName);
counter += currValue; //custom sum aggregate, implement aggregation here
}
e.Result = msg + counter;
}
if (colName == "someColumn")
{
TimeSpan totalTime = new TimeSpan(0);
GridItem[] groupChildItems = correspondingHeader.GetChildItems();
for (int i = 0; i < groupChildItems.Length; i++)
{
TimeSpan currValue = (TimeSpan)DataBinder.Eval((groupChildItems[i] as GridDataItem).DataItem, colName);
totalTime = totalTime.Add(currValue);
}
e.Result = msg + totalTime;
}
}
//the total grid footer that is not specific to a group, we need to aggregate all data
if (e.Item is GridFooterItem)
{
string msg = "Grid total footer aggregate: ";
if (colName == "moreData")
{
decimal counter = 0;
//Loop all items in the current grid to aggregate
foreach (GridDataItem item in RadGrid1.MasterTableView.Items)
{
decimal currValue = (decimal)item.GetDataKeyValue(colName);
counter += currValue;
}
e.Result = msg + counter;
}
if (colName == "someColumn")
{
TimeSpan totalTime = new TimeSpan(0);
foreach (GridDataItem item in RadGrid1.MasterTableView.Items)
{
TimeSpan currValue = (TimeSpan)item.GetDataKeyValue(colName);
totalTime = totalTime.Add(currValue);
}
e.Result = msg + totalTime;
}
}
}