using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

 

using System.Collections;

 

namespace GridViews

{

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

    {

        private const int PRODUCTID_COLUMN = 0;

        private const int PRICE_COLUMN = 2;

        private const int FIRST_CONTROL = 0;

 

        ArrayList arrProducts = new ArrayList();

 

        protected void Page_Load(object sender, EventArgs e)

        {

            if (!IsPostBack)

            {

                GenerateArrayList();

                Session["Products"] = arrProducts;

                ShowProducts();

            }

        }

 

        public void ShowProducts()

        {

            arrProducts = (ArrayList)Session["Products"];

            gvProducts.DataSource = arrProducts;

            gvProducts.DataBind();

        }

 

        public void GenerateArrayList()

        {

            Product objProduct;

 

            for (int i = 1; i <= 6; i++)

            {

                objProduct = new Product();

                objProduct.ProductID = (100 + i).ToString();

                objProduct.Description = "Product " + i.ToString();

                objProduct.Price = 3.5 * i;

                objProduct.QOH = 5 * i;

                arrProducts.Add(objProduct);

            }

        }

 

        // CommandField Select Button event handling code

        // Double-click on the select button to add this event handler

        protected void gvProducts_SelectedIndexChanged(object sender, EventArgs e)

        {

            // Retrieve and display the value contained in the

            // BoundField for ProductNumber (column index 0) of the row the button was clicked

            lblDisplay.Text = "ProductNumber: " + gvProducts.SelectedRow.Cells[PRODUCTID_COLUMN].Text;

        }

 

        // RowEditing event handler that fires when the CommandField Edit button is clicked.

        // There is no double-click that will produce this handler

        protected void gvProducts_RowEditing(Object sender, System.Web.UI.WebControls.GridViewEditEventArgs e)

        {

            // Set the row to edit-mode in the GridView

            gvProducts.EditIndex = e.NewEditIndex;

 

            ShowProducts();

 

            lblDisplay.Text = "";

        }

 

        // RowUpdating event handler that fires when the CommandField Update button is clicked.

        // There is no double-click that will produce this handler

        protected void gvProducts_RowUpdating(Object sender, System.Web.UI.WebControls.GridViewUpdateEventArgs e)

        {

            // retrieve the row index for which the Update button was clicked

            // and retrieve the ProductNumber from the first column (BoundField) in that row.

            int rowIndex = e.RowIndex;

            String prodNum = gvProducts.Rows[rowIndex].Cells[PRODUCTID_COLUMN].Text;

 

            // Retrieve the Product object the selected row refers to in the arraylist

            arrProducts = (ArrayList)Session["Products"];

            Product objProduct = (Product)arrProducts[rowIndex];

 

            // Retrieve a reference to the TextBox in the row for the quantity to add

            TextBox TBox;

            TBox = (TextBox)gvProducts.Rows[rowIndex].FindControl("txtQtyAdd");

            int quantity = int.Parse(TBox.Text);

 

            // Retrieve a reference to a TextBox created by the GridView when it's in edit-mode

            TBox = (TextBox)gvProducts.Rows[rowIndex].Cells[PRICE_COLUMN].Controls[FIRST_CONTROL];

            double price = double.Parse(TBox.Text);

 

            // Update the object and update the arraylist stored in the Session

            objProduct.Price = price;

            objProduct.QOH = objProduct.QOH + quantity;

            Session["Products"] = arrProducts;

 

            lblDisplay.Text = "Product " + prodNum + " has been updated.";

 

            // Set the GridView back to the original state.

            // No rows currently being edited.

            gvProducts.EditIndex = -1;

 

            ShowProducts();

        }

 

        // RowCancelingEdit event handler that fires when the CommandField Cancel button is clicked.

        // There is no double-click that will produce this handler

        protected void gvProducts_RowCancelingEdit(Object sender, System.Web.UI.WebControls.GridViewCancelEditEventArgs e)

        {

            // Set the GridView back to the original state

            // No rows currently being editted

            gvProducts.EditIndex = -1;

 

            ShowProducts();

 

            lblDisplay.Text = "";

        }

    }

}