当前位置: 首页 > 编程语言 > C#

使用GridViewShare生成数据表

时间:2023-04-10 17:11:15 C#

使用GridView生成数据表我有一个网格视图,在网格视图中,我有一个按钮,当按钮被选中时,我需要在数据表中插入/更新记录.如果数据表中有值,则qty字段自增1。在数据表中插入数量为1的新行。现在问题出在我正在编写这段代码的GridView1_RowCommand上。但它在数据表中给出了错误的值。我的代码写在下面。请帮我。protectedvoidGridView1_RowCommand(objectsender,GridViewCommandEventArgse){if(e.CommandName=="datacommand"){DataTabledt=newDataTable();if(Session["product_id"]!=null){dt=(DataTable)Session["product_id"];}数据行博士;//dt.Rows[0]["数量"]=数据;如果(dt.Rows.Count<=0){dt.Columns.Add("product_id",typeof(Int32));dt.Columns.Add("数量",typeof(int));dt.Columns.Add("价格",typeof(double));dt.Columns.Add("总计",typeof(double));dr=dt.NewRow();dr["product_id"]=e.CommandArgument;博士[“数量”]=1;dr["价格"]=Convert.ToDouble(GridView1.Rows[0].Cells[3].Text);dr["总计"]=Convert.ToInt32(dr["qty"])*Convert.ToDouble(dr["价格"]);dt.Rows.Add(博士);dt.AcceptChanges();会话["product_id"]=dt;Response.Write("一次");}else{//dt=Session["product_id"];for(inti=0;ip.product_id==productId);if(isexist){myclassproduct=productDetails.Where(p=>p.product_id==productId).FirstOrDefault();productDetails.Add(newmyclass{product_id=product.product_id,price=product.price,qty=product.qty+1,total=Convert.ToDouble((product.price)*(product.qty+1))});}else{doubleproductPrice=Convert.ToDouble(GridView1.Rows[0].Cells[3].Text);双productTotal=productPrice*1;productDetails.Add(newmyclass{product_id=Convert.ToInt32(e.CommandArgument.ToString()),price=productPrice,qty=1,total=productTotal});}}}本文收集自网络,不代表立场。如涉及侵权,请点击右侧联系管理员删除。如有转载请注明出处: