using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using
Utilities;
namespace GridViews
{
public partial class GridViewExample3 : System.Web.UI.Page
{
DBConnect objDB = new DBConnect();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
ShowProducts();
}
public void ShowProducts()
{
String strSQL = "SELECT * FROM Product";
gvProducts.DataSource
= objDB.GetDataSet(strSQL);
gvProducts.DataBind();
}
// 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[0].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[0].Text;
//
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[2].Controls[0];
double price =
double.Parse(TBox.Text);
String strSQL = "UPDATE Product SET QOH = QOH + " + quantity + ",
" +
"Price = " + price
+ " WHERE ProductNumber
= '" +prodNum + "'";
objDB.DoUpdate(strSQL);
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
= "";
}
}
}