using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Data;
using System.Configuration;
using System.Web.Security;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Telerik.Web.UI;
using System.Drawing;

public partial class Default : System.Web.UI.Page
{
    protected void Page_Init(object sender, EventArgs e)
    {
        RadGrid grid = new RadGrid();
        grid.AutoGenerateColumns = false;
        grid.DataSourceID = "SqlDataSource1";
        grid.ShowFooter = true;
        string templateColumnName = "UnitPrice";
        GridTemplateColumn templateColumn = new GridTemplateColumn();

        templateColumn.ItemTemplate = new MyTemplate(templateColumnName);
        templateColumn.FooterTemplate = new MyFooterTemplate(templateColumnName);
        templateColumn.HeaderText = templateColumnName;
        GridBoundColumn boundColumn1 = new GridBoundColumn();
        boundColumn1.DataField = "Quantity";
        boundColumn1.UniqueName = "Quantity";
        boundColumn1.HeaderText = "Bound Column";
        grid.MasterTableView.Columns.Add(templateColumn);
        grid.MasterTableView.Columns.Add(boundColumn1);
        grid.AllowPaging = true;
        grid.PageSize = 10;
        grid.Skin = "Outlook";
        grid.PreRender += new EventHandler(grid_PreRender);
        PlaceHolder1.Controls.Add(grid);
    }

    void grid_PreRender(object sender, EventArgs e)
    {
        RadGrid radGrid1 = sender as RadGrid;
        decimal sum = 0;
        decimal avr = 0;
        decimal min = decimal.MaxValue;
        decimal max = decimal.MinValue;
        int count = 0;

        foreach (GridDataItem item in radGrid1.MasterTableView.Items)
        {
            Label label1 = item.FindControl("Label1") as Label;
            decimal currentPrice = decimal.Parse(label1.Text);
            sum += currentPrice;

            if (currentPrice < min)
            {
                min = currentPrice;
            }

            if (currentPrice > max)
            {
                max = currentPrice;
            }
            count++;
        }

        avr = sum / count;
        GridFooterItem footer = radGrid1.MasterTableView.GetItems(GridItemType.Footer)[0] as GridFooterItem;
        (footer.FindControl("templateFooterColumnTextBox1") as TextBox).Text = sum.ToString();
        (footer.FindControl("templateFooterColumnTextBox2") as TextBox).Text = avr.ToString();
        (footer.FindControl("templateFooterColumnTextBox3") as TextBox).Text = min.ToString();
        (footer.FindControl("templateFooterColumnTextBox4") as TextBox).Text = max.ToString();
    }

    private class MyFooterTemplate : ITemplate
    {
        private string colname;
        protected TextBox textBox1;
        protected TextBox textBox2;
        protected TextBox textBox3;
        protected TextBox textBox4;

        public MyFooterTemplate(string cName)
        {
            colname = cName;
        }

        public void InstantiateIn(System.Web.UI.Control container)
        {
            textBox1 = new TextBox();
            textBox2 = new TextBox();
            textBox3 = new TextBox();
            textBox4 = new TextBox();
            textBox1.ID = "templateFooterColumnTextBox1";
            textBox2.ID = "templateFooterColumnTextBox2";
            textBox3.ID = "templateFooterColumnTextBox3";
            textBox4.ID = "templateFooterColumnTextBox4";
            Table table = new Table();
            TableRow row1 = new TableRow();
            TableRow row2 = new TableRow();
            TableRow row3 = new TableRow();
            TableRow row4 = new TableRow();
            TableCell cell11 = new TableCell();
            TableCell cell12 = new TableCell();
            TableCell cell21 = new TableCell();
            TableCell cell22 = new TableCell();
            TableCell cell31 = new TableCell();
            TableCell cell32 = new TableCell();
            TableCell cell41 = new TableCell();
            TableCell cell42 = new TableCell();
            row1.Cells.Add(cell11);
            row1.Cells.Add(cell12);
            row2.Cells.Add(cell21);
            row2.Cells.Add(cell22);
            row3.Cells.Add(cell31);
            row3.Cells.Add(cell32);
            row4.Cells.Add(cell41);
            row4.Cells.Add(cell42);
            cell11.BorderStyle = BorderStyle.None;
            cell12.BorderStyle = BorderStyle.None;
            cell21.BorderStyle = BorderStyle.None;
            cell22.BorderStyle = BorderStyle.None;
            cell31.BorderStyle = BorderStyle.None;
            cell32.BorderStyle = BorderStyle.None;
            cell41.BorderStyle = BorderStyle.None;
            cell42.BorderStyle = BorderStyle.None;
            table.Rows.Add(row1);
            table.Rows.Add(row2);
            table.Rows.Add(row3);
            table.Rows.Add(row4);
            cell11.Text = "Sum:";
            cell12.Controls.Add(textBox1);
            cell21.Text = "Average:";
            cell22.Controls.Add(textBox2);
            cell31.Text = "Min Price:";
            cell32.Controls.Add(textBox3);
            cell41.Text = "Max Price:";
            cell42.Controls.Add(textBox4);
            container.Controls.Add(table);


        }
    }

    private class MyTemplate : ITemplate
    {
        private string colname;
        protected Label label1;

        public MyTemplate(string cName)
        {
            colname = cName;
        }

        public void InstantiateIn(System.Web.UI.Control container)
        {
            label1 = new Label();
            label1.ID = "Label1";
            label1.DataBinding += new EventHandler(label1_DataBinding);
            container.Controls.Add(label1);
        }

        void label1_DataBinding(object sender, EventArgs e)
        {
            Label label = (Label)sender;
            GridDataItem container = (GridDataItem)label.NamingContainer;
            label.Text = (((DataRowView)container.DataItem)[colname]).ToString();
        }
    }
}
