Hi
I have a hierarchical grid where some of the columns I want to total but the total to be at the top in the header. I have a working set of code below which shows the hierarchical grid and I want to sum the 3 columns "ValueToSum","ValueToSum2" and "ValueToSum3". I would like the totals in the group headers (both levels).
If this is not possible then do say, thanks
ASPX
.cs Code Behind
I have a hierarchical grid where some of the columns I want to total but the total to be at the top in the header. I have a working set of code below which shows the hierarchical grid and I want to sum the 3 columns "ValueToSum","ValueToSum2" and "ValueToSum3". I would like the totals in the group headers (both levels).
If this is not possible then do say, thanks
ASPX
<%@ Page Title="Home Page" Language="C#" AutoEventWireup="true"
CodeBehind="Default.aspx.cs" Inherits="DataFormatting._Default" %>
<%@ Register assembly="Telerik.Web.UI" namespace="Telerik.Web.UI" tagprefix="telerik" %>
<
form
id
=
"form1"
runat
=
"server"
>
<
telerik:RadAjaxManager
ID
=
"RadAjaxManager1"
runat
=
"server"
>
</
telerik:RadAjaxManager
>
<
telerik:RadScriptManager
ID
=
"RadScriptManager1"
Runat
=
"server"
>
</
telerik:RadScriptManager
>
<
div
class
=
"contributionTable"
>
<
asp:PlaceHolder
ID
=
"PlaceHolder1"
runat
=
"server"
>
<
telerik:RadGrid
ID
=
"clientContribution"
runat
=
"server"
onitemdatabound
=
"clientContribution_ItemDataBound"
>
</
telerik:RadGrid
>
</
asp:PlaceHolder
>
</
div
>
</
form
>
.cs Code Behind
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Diagnostics;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
namespace DataFormatting
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
BuildTheGrid();
}
}
protected void clientContribution_ItemDataBound(object sender, GridItemEventArgs e)
{
if (e.Item is GridGroupHeaderItem)
{
GridGroupHeaderItem item = (GridGroupHeaderItem)e.Item;
DataRowView groupDataRow = (DataRowView)e.Item.DataItem;
}
}
#region Build the grid
private DataSet BuildTheGrid()
{
try
{
clientContribution.DataSource = null;
DataSet contributionColumns = LoadGridData();
// Define the main grid
for (int loopPos = clientContribution.MasterTableView.Columns.Count; loopPos > 0; loopPos--)
{
clientContribution.MasterTableView.Columns.RemoveAt(loopPos - 1);
}
clientContribution.ID = "MyGrid";
clientContribution.DataSource = contributionColumns;
clientContribution.Width = Unit.Percentage(98);
clientContribution.AutoGenerateColumns = false;
clientContribution.ShowStatusBar = true;
clientContribution.MasterTableView.Width = Unit.Percentage(100);
// now build the hierarchical grid
clientContribution.MasterTableView.GroupByExpressions.Add(
new GridGroupByExpression("GroupValue1 group by GroupValue1"));
clientContribution.MasterTableView.GroupByExpressions.Add(
new GridGroupByExpression("GroupValue2 group by GroupValue2"));
GridBoundColumn boundColumn = new GridBoundColumn();
foreach (DataColumn col in contributionColumns.Tables[0].Columns)
{
boundColumn = new GridBoundColumn();
clientContribution.MasterTableView.Columns.Add(boundColumn);
boundColumn.DataField = col.ColumnName;
boundColumn.HeaderText = col.ColumnName;
boundColumn.Visible = true;
}
clientContribution.DataBind();
}
catch (Exception exc)
{
Debug.WriteLine(exc.Message);
return null;
}
finally
{
}
return null;
}
#endregion
#region Load the Grid Data
private DataSet LoadGridData()
{
// return the data to display
DataSet contributionData = new DataSet("MyData");
DataTable gridData = contributionData.Tables.Add("ContData");
gridData.Columns.Add(new DataColumn("GroupValue1"));
gridData.Columns.Add(new DataColumn("GroupValue2"));
DataColumn decCol = new DataColumn("ValueToSum");
decCol.DataType = System.Type.GetType("System.Int32");
gridData.Columns.Add(decCol);
DataColumn perCol = new DataColumn("ValueToIgnore");
perCol.DataType = System.Type.GetType("System.Int32");
gridData.Columns.Add(perCol);
DataColumn decCol2 = new DataColumn("ValueToSum2");
decCol2.DataType = System.Type.GetType("System.Int32");
gridData.Columns.Add(decCol2);
DataColumn perCol2 = new DataColumn("ValueToIgnore2");
perCol2.DataType = System.Type.GetType("System.Int32");
gridData.Columns.Add(perCol2);
DataColumn decCol3 = new DataColumn("ValueToSum3");
decCol3.DataType = System.Type.GetType("System.Int32");
gridData.Columns.Add(decCol3);
DataColumn perCol3 = new DataColumn("ValueToIgnore3");
perCol3.DataType = System.Type.GetType("System.Int32");
gridData.Columns.Add(perCol3);
for (int j = 1; j < 5; j++)
{
for (int i = 1; i < 5; i++)
{
DataRow dataRow = contributionData.Tables[0].NewRow();
dataRow["GroupValue1"] = "Header 1 as " + j.ToString();
dataRow["GroupValue2"] = "Heading 2 as " + j.ToString();
dataRow["ValueToSum"] = 1234 * i * j;
dataRow["ValueToIgnore"] = 805 * i * j;
dataRow["ValueToSum2"] = 42 * i * j;
dataRow["ValueToIgnore2"] = 901 * i * j;
dataRow["ValueToSum3"] = 651 * i * j;
dataRow["ValueToIgnore3"] = 104 * i * j;
contributionData.Tables[0].Rows.Add(dataRow);
}
}
return contributionData;
}
#endregion
}
}