New to Telerik UI for ASP.NET AJAX? Start a free 30-day trial
Exclude RadGrid Row from Column with custom Aggregation
Environment
Version | 2023.3.1010 |
Product | RadGrid for ASP.NET AJAX |
Description
This example demonstrates how to exclude a row from a column with custom aggregation. The below code snippet uses rows of numeric and DateTime values and has its Aggregate
property set to 'custom':
Solution
ASPX
<telerik:RadGrid
ID="RadGrid1"
runat="server"
AllowPaging="True"
Width="800px"
OnNeedDataSource="RadGrid1_NeedDataSource"
ShowFooter="true">
<MasterTableView AutoGenerateColumns="False">
<Columns>
<telerik:GridBoundColumn
DataField="MyField"
DataType="System.String"
Aggregate="Custom">
</telerik:GridBoundColumn>
</Columns>
</MasterTableView>
</telerik:RadGrid>
C#
private DataTable OrdersTable() // Creating a collumn with rows of numeric and DateTime values
{
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("MyField", typeof(string)));
for (int i = 0; i < 5; i++)
{
int index = i + 1;
DataRow row = dt.NewRow();
if (index == 2 || index == 4)
{
row["MyField"] = DateTime.Now.Date.ToString();
}
else
{
row["MyField"] = index.ToString();
}
dt.Rows.Add(row);
}
return dt; // This will return a test grid with one column with 5 rows, 2 of which are dates
}
protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
{
RadGrid grid = sender as RadGrid;
DataTable sourceData = OrdersTable();
grid.DataSource = sourceData;
double sum = 0;
foreach (DataRow row in sourceData.Rows)
{
double numericValue;
if (double.TryParse(row["MyField"].ToString(), out numericValue)) // If the value in the row is a number, add it to the aggregated sum.
{
sum += Convert.ToDouble(row["MyField"]);
}
}
grid.MasterTableView.GetColumn("MyField").FooterText = "Sum: " + sum.ToString();
}