I am working on Add To Cart Functionality, and I have added a GridView
control with paging enabled. In the GridView
, I show the quantity in a text box and handle the OnTextChanged
event for that textbox. Now the problem, is how can I keep the changed quantity text in session or view state, and in which row, so that I can update my GridView
and bind that data again to the GridView
?
Here I took GridView
with id gvMaster.
protected void txtQuantity_TextChanged(object sender, EventArgs e)
{
gvMaster.DataSource = ProductDetailsGridMaster();
gvMaster.AllowPaging = true;
gvMaster.DataBind();
}
public DataTable ProductDetailsGridMaster()
{
DataTable dtProducts = new DataTable();
dtProducts.Columns.Add("ProductId");
dtProducts.Columns.Add("ProductName");
dtProducts.Columns.Add("ProductPrice");
dtProducts.Columns.Add("Quantity");
dtProducts.Columns.Add("Price");
gvMaster.AllowPaging = false;
if (Session["dtProducts"] != null)
{
GridView gv = new GridView();
gv.DataSource = Session["dtProducts"];
gvMaster.DataSource = gv.DataSource;
gvMaster.DataBind();
lblMessage.Text = "";
}
//GridView gvc = (GridView)Page.FindControl("gvMaster");
for (int i = 0; i < gvMaster.Rows.Count; i++)
{
Label lblProductId = (Label)gvMaster.Rows[i].Cells[0].FindControl("lblProductId");
Label lblProductName = (Label)gvMaster.Rows[i].Cells[1].FindControl("lblProductName");
Label lblProductPrice = (Label)gvMaster.Rows[i].Cells[2].FindControl("lblProductPrice");
//Label lblssno = (Label)gv.Rows[i].Cells[2].FindControl("lblSSNo");
TextBox txtQuantity = (TextBox)gvMaster.Rows[i].Cells[3].FindControl("txtQuantity");
//TextBox mastertxtQuantity = (TextBox)gvMaster.Rows[i].Cells[3].FindControl("txtQuantity");
Label lblPrice = (Label)gvMaster.Rows[i].Cells[4].FindControl("lblPrice");
var Price = decimal.Parse(lblProductPrice.Text) * decimal.Parse(txtQuantity.Text);
lblPrice.Text = Price.ToString();
DataRow dr = dtProducts.NewRow();
dr["ProductId"] = lblProductId.Text;
dr["ProductName"] = lblProductName.Text;
dr["ProductPrice"] = lblProductPrice.Text;
dr["Quantity"] = txtQuantity.Text;
dr["Price"] = lblPrice.Text;
dtProducts.Rows.Add(dr);
}
Session["dtProducts"] = dtProducts;
return dtProducts;
}
I want to show changed quantity value in grid with paging enabled.