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

Exclude certain rows from Column Aggregate

Description

Sometimes one might want to exclude specific rows from being aggregated.

"Example figure with custom aggregate"

This will require using custom aggregation.

Solution

  1. Set the Aggregate property of the Column to Custom.
  2. Bind the OnCustomAggregate event to the Grid.
  3. In the event handler, you can calculate the values you want and update the footer accordingly

Example

ASPX
<telerik:RadGrid
    ID="RadGrid1"
    runat="server"
    AllowPaging="True"
    Width="800px"
    OnNeedDataSource="RadGrid1_NeedDataSource"
    OnCustomAggregate="RadGrid1_CustomAggregate"
    MasterTableView-ShowGroupFooter="true"
    ShowGroupPanel="true"
    ShowFooter="true">
    <ClientSettings AllowDragToGroup="true">
    </ClientSettings>
    <GroupingSettings ShowUnGroupButton="true" />
    <MasterTableView AutoGenerateColumns="false">
        <GroupByExpressions>
            <telerik:GridGroupByExpression>
                <GroupByFields>
                    <telerik:GridGroupByField FieldName="Group" />
                </GroupByFields>

                <SelectFields>
                    <telerik:GridGroupByField FieldName="Group" />
                </SelectFields>
            </telerik:GridGroupByExpression>
        </GroupByExpressions>
        <Columns>
            <telerik:GridBoundColumn DataField="MyField" HeaderText="MyField" DataType="System.String" Aggregate="Custom" />
            <telerik:GridBoundColumn DataField="Group" HeaderText="Group" />
        </Columns>
    </MasterTableView>
</telerik:RadGrid>
C#
	private DataTable OrdersTable()
	{
		DataTable dt = new DataTable();

		dt.Columns.Add(new DataColumn("MyField", typeof(string)));
		dt.Columns.Add(new DataColumn("Group", typeof(string)));

		for (int i = 0; i < 10; i++)
		{
			int index = i + 1;

			DataRow row = dt.NewRow();

			row["Group"] = index % 2 == 0 ? "Group 1" : "Group 2";

			if (index == 2)
			{
				row["MyField"] = DateTime.Now.Date.ToString();
			}
			else
			{
				row["MyField"] = 1;
			}

			dt.Rows.Add(row);
		}

		return dt;
	}

	protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
	{
		RadGrid grid = sender as RadGrid;
		DataTable sourceData = OrdersTable();
		grid.DataSource = sourceData;
	}

	protected void RadGrid1_CustomAggregate(object sender, GridCustomAggregateEventArgs e)
	{
		if (e.Column.UniqueName == "MyField")
		{
			if (e.Item is GridGroupFooterItem) // Custom Aggregate for the items within the Group
			{
				GridGroupFooterItem groupFooterItem = (GridGroupFooterItem)e.Item;
				GridGroupHeaderItem groupHeaderItem = groupFooterItem.GroupHeaderItem;

				GridItem[] groupChildItems = groupHeaderItem.GetChildItems();

				decimal counter = 0;

				for (int i = 0; i < groupChildItems.Length; i++)
				{
					GridDataItem dataItem = groupChildItems[i] as GridDataItem;

					if (dataItem != null)
					{
						decimal numericValue = 0;

						if (decimal.TryParse(DataBinder.Eval(dataItem.DataItem, "MyField").ToString(), out numericValue))
						{
							counter += numericValue;
						}
					}
				}
				e.Result = counter;
			}
			else if (e.Item is GridFooterItem) // Custom Aggregate for all items in the Grid
			{
				double sum = 0;

				DataTable dt = RadGrid1.DataSource as DataTable;

				foreach (DataRow row in dt.Rows)
				{
					double numericValue;

					if (double.TryParse(row["MyField"].ToString(), out numericValue))
					{
						sum += numericValue;
					}
				}
				e.Result = sum;
			}
		}
	}
In this article
DescriptionSolution
Not finding the help you need?
Contact Support