using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

 

using Utilities;

using System.Globalization;     // required for NumberStyles and converting strings with $ to numbers

using System.Drawing;           // required for using Color values

 

namespace GridViews

{

    public partial class GridViewExample4 : System.Web.UI.Page

    {

        private const int FIRST_COLUMN = 0;

        private const int QOH_COLUMN = 2;

        private const int PRICE_COLUMN = 3;

 

        DBConnect objDB = new DBConnect();

 

        protected void Page_Load(object sender, EventArgs e)

        {

            if (!IsPostBack)

            {

                String strSQL = "SELECT * FROM Product";

                int count = 0;

                int totalQuantity = 0;

                double total = 0;

 

                gvProducts.DataSource = objDB.GetDataSet(strSQL, out count);

                gvProducts.DataBind();

 

                // Computer the totals by summing the values in the QOH and Price columns

                // for each row in the GridView

                for (int i = 0; i < count; i++)

                {

                    totalQuantity = totalQuantity + int.Parse(gvProducts.Rows[i].Cells[QOH_COLUMN].Text);

                    total = total + double.Parse(gvProducts.Rows[i].Cells[PRICE_COLUMN].Text, NumberStyles.Currency);

                }

 

                // Put the values into the corresponding footer column

                gvProducts.Columns[FIRST_COLUMN].FooterText = "Totals:";

                gvProducts.Columns[QOH_COLUMN].FooterText = totalQuantity.ToString();

                gvProducts.Columns[PRICE_COLUMN].FooterText = total.ToString("C2");     //C2 formats as currency

                gvProducts.Columns[FIRST_COLUMN].FooterStyle.ForeColor = Color.Red;

                gvProducts.Columns[QOH_COLUMN].FooterStyle.ForeColor = Color.Red;

                gvProducts.Columns[PRICE_COLUMN].FooterStyle.ForeColor = Color.Red;

 

                // Re-Bind the GridView with the changes made to the footer

                gvProducts.DataBind();

 

            }

        }

    }

}