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

public partial class DataGrid_RowSummary : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

        //Define SQL query
        string SqlQuery = @"SELECT ProductID, ProductName, UnitPrice, UnitsInStock, UnitsOnOrder FROM Products ORDER BY ProductID";

        //Get DataTable (MS Access Database)
        DataTable myDataSource = DataBase.GetDataTableOleDb(SqlQuery, "~/DataGrid/DataBases/Nwind.mdb");

        //Set the data source
        myDataGrid.KeyFieldName = "ProductID";
        myDataGrid.DataSource = myDataSource;
        myDataGrid.DataBind();


        //Compute summary data
        decimal UnitPrice_Sum = (decimal)myDataGrid.Columns["UnitPrice"].Sum();
        decimal UnitPrice_Min = (decimal)myDataGrid.Columns["UnitPrice"].Minimum();
        decimal UnitPrice_Max = (decimal)myDataGrid.Columns["UnitPrice"].Maximum();
        long UnitsInStock_Sum = (long)myDataGrid.Columns["UnitsInStock"].Sum();
        short UnitsOnOrder_Min = (short)myDataGrid.Columns["UnitsOnOrder"].Minimum();
        short UnitsOnOrder_Max = (short)myDataGrid.Columns["UnitsOnOrder"].Maximum();
        short UnitsOnOrder_Avg = (short)myDataGrid.Columns["UnitsOnOrder"].Average();


        //Add summary data to the columns
        myDataGrid.Columns["ProductID"].Summary = "Products: <b>" + myDataGrid.Rows.Count.ToString() + "</b>";
        myDataGrid.Columns["ProductName"].Summary = "Product Categories: <b>Beverages, Condiments, Confections.</b>";
        
        myDataGrid.Columns["UnitPrice"].Summary = String.Format("Total: <font color=\"#124312\"><b>{0:C}</b></font>", UnitPrice_Sum) +
            String.Format("<br/>Max: <font color=\"#000059\"><b>{0:C}</b></font>", UnitPrice_Max) +
            String.Format("<br/>Min: <font color=\"#710F0F\"><b>{0:C}</b></font>", UnitPrice_Min);
        
        myDataGrid.Columns["UnitsInStock"].Summary = "Total: <b>" + UnitsInStock_Sum.ToString() + "</b>";
        myDataGrid.Columns["UnitsOnOrder"].Summary = "Max: <b>" + UnitsOnOrder_Max.ToString() + "</b>" +
            "<br/>Min: <b>" + UnitsOnOrder_Min.ToString() + "</b>" +
            "<br/>Avg: <b>" + UnitsOnOrder_Avg.ToString() + "</b>";

    }
}