主题:大家快来帮帮忙吧,小弟快要急死了
大家好,我最近在用GridView做一个购物车的例子。
GridView的前3列是绑定列,分别绑定了商品编号、商品名称和商品价格。
第4列是一个模板列,里面有一个TextBox用于存放商品数量(ID=txtProductNum)和一个Button用于更新商品数量(ID=btnChangeNum)。
我现在的问题是在TextBox里输入修改后的商品数量,怎么样用Button把这个值更新到数据库里?
我是新手,对GridView运用的还不是很熟练,而且还有很多技巧不会,希望大家能帮忙指点一下,谢谢了!
下面是我写的更新代码,但是报错说不包含IndexRow,迷茫中。。。
protected void btnChangeNum_Click(object sender, EventArgs e)
{
con.Open();
GridViewRow gvr = GVShoppingCart.Rows[e.RowIndex];
string productID = Convert.ToString(gvr.Cells[0].Text);
string productNum = ((TextBox)(gvr.Cells[3].Controls[0])).Text;
Response.Write(productID);
Response.Write(productNum);
string sqlstrUpdate = "update T_ShoppingCart_Info set productNum='" + productNum + "'where productID='" + productID + "'";
SqlCommand cmdUpdate = new SqlCommand(sqlstrUpdate, con);
cmdUpdate.ExecuteNonQuery();
con.Close();
BindToGVShoppingCart();
}
GridView的前3列是绑定列,分别绑定了商品编号、商品名称和商品价格。
第4列是一个模板列,里面有一个TextBox用于存放商品数量(ID=txtProductNum)和一个Button用于更新商品数量(ID=btnChangeNum)。
我现在的问题是在TextBox里输入修改后的商品数量,怎么样用Button把这个值更新到数据库里?
我是新手,对GridView运用的还不是很熟练,而且还有很多技巧不会,希望大家能帮忙指点一下,谢谢了!
下面是我写的更新代码,但是报错说不包含IndexRow,迷茫中。。。
protected void btnChangeNum_Click(object sender, EventArgs e)
{
con.Open();
GridViewRow gvr = GVShoppingCart.Rows[e.RowIndex];
string productID = Convert.ToString(gvr.Cells[0].Text);
string productNum = ((TextBox)(gvr.Cells[3].Controls[0])).Text;
Response.Write(productID);
Response.Write(productNum);
string sqlstrUpdate = "update T_ShoppingCart_Info set productNum='" + productNum + "'where productID='" + productID + "'";
SqlCommand cmdUpdate = new SqlCommand(sqlstrUpdate, con);
cmdUpdate.ExecuteNonQuery();
con.Close();
BindToGVShoppingCart();
}