我正在使用网格视图,在该网格视图中我有一个按钮,在选择该按钮时,我需要在数据表中插入/更新记录。如果在数据表中,如果有值,则qty字段将增加1。在数据表中插入带有数量1的新行。现在的事情是GridView1_RowCommand我正在编写这段代码。但它在数据表中给出了错误的值。我的代码写在下面。请帮我。
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "datacommand")
{
DataTable dt = new DataTable();
if (Session["product_id"] != null)
{
dt = (DataTable)Session["product_id"];
}
DataRow dr;
//dt.Rows[0]["qty"] = data;
if (dt.Rows.Count<=0)
{
dt.Columns.Add("product_id", typeof(Int32));
dt.Columns.Add("qty", typeof(int));
dt.Columns.Add("price", typeof(double));
dt.Columns.Add("total", typeof(double));
dr = dt.NewRow();
dr["product_id"] = e.CommandArgument;
dr["qty"] = 1;
dr["price"] = Convert.ToDouble(GridView1.Rows[0].Cells[3].Text);
dr["total"] = Convert.ToInt32(dr["qty"]) * Convert.ToDouble(dr["price"]);
dt.Rows.Add(dr);
dt.AcceptChanges();
Session["product_id"] = dt;
Response.Write("<script type='javacript'> One time</script>");
}
else
{
//dt = Session["product_id"];
for (int i = 0; i < dt.Rows.Count; i++)
{
if (dt.Rows[i]["product_id"].ToString() == e.CommandArgument)
{
dr = dt.NewRow();
dt.Rows[i]["qty"] = Convert.ToInt32(dt.Rows[i]["qty"])+ 1;
dt.Rows[i]["total"] = Convert.ToInt32(dt.Rows[i]["qty"]) * Convert.ToDouble(dt.Rows[i]["price"]);
Session["product_id"] = dt;
dt.AcceptChanges();
Response.Write(dt);
}
}
dr = dt.NewRow();
dr["product_id"] = e.CommandArgument;
dr["qty"] = 1;
dr["price"] = Convert.ToDouble(GridView1.Rows[0].Cells[3].Text);
dr["total"] = Convert.ToInt32(dr["qty"]) * Convert.ToDouble(dr["price"]);
dt.Rows.Add(dr);
dt.AcceptChanges();
Session["product_id"] = dt;
}
//GridViewRow row= e.CommandArgument
////DataColumn prodid = new DataColumn("product_id", typeof(System.Int32));
////dt.Columns.Add(prodid);
////DataColumn qty = new DataColumn("qty", typeof(System.Int32));
////dt.Columns.Add(qty);
//int index = Convert.ToInt32(e.CommandArgument);
//GridViewRow row = GridView1.Rows[index];
//AddShopCart(row.Cells[1].Text.ToString());
}
}
答案 0 :(得分:1)
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "datacommand")
{
DataTable dt = new DataTable();
if (Session["product_id"] != null)
{
dt = (DataTable)Session["product_id"];
}
DataRow dr;
if (dt.Rows.Count<=0)
{
dt.Columns.Add("product_id", typeof(Int32));
dt.Columns.Add("qty", typeof(int));
dt.Columns.Add("price", typeof(double));
dt.Columns.Add("total", typeof(double));
dr = dt.NewRow();
dr["product_id"] = e.CommandArgument;
dr["qty"] = 1;
dr["price"] = Convert.ToDouble(GridView1.Rows[0].Cells[3].Text);
dr["total"] = Convert.ToInt32(dr["qty"]) * Convert.ToDouble(dr["price"]);
dt.Rows.Add(dr);
dt.AcceptChanges();
Session["product_id"] = dt;
Response.Write("<script type='javacript'> One time</script>");
}
else
{
string aa="new";
for (int i = 0; i < dt.Rows.Count; i++)
{
if (dt.Rows[i]["product_id"].ToString() == e.CommandArgument)
{
aa="dup";
}
}
if(aa=="dup")
{
for (int j = 0; j < dt.Rows.Count; j++)
{
if (dt.Rows[j]["product_id"].ToString() == e.CommandArgument)
{
// aa="dup";
dt.Rows[j]["qty"]=Convert.ToString( Convert.ToInt32( dt.Rows[j]["qty"])+1);
dt.AcceptChanges();
}
}
Session["product_id"]=dt;
}
else
{
dt.Columns.Add("product_id", typeof(Int32));
dt.Columns.Add("qty", typeof(int));
dt.Columns.Add("price", typeof(double));
dt.Columns.Add("total", typeof(double));
DataRow dr1=dt.NewRow() ;
dr1["product_id"] = e.CommandArgument;
dr1["qty"] = 1;
dr1["price"] = Convert.ToDouble(GridView1.Rows[0].Cells[3].Text);
dr1["total"] = Convert.ToInt32(dr["qty"]) * Convert.ToDouble(dr["price"]);
dt.Rows.Add(dr);
dt.AcceptChanges();
Session["product_id"] = dt;
}
}
}
}
所以这就是答案
答案 1 :(得分:-1)
首先为属性创建类, 你可以修改它然后使用。
public class myclass
{
public int product_id { get; set; }
public int qty { get; set; }
public double price { get; set; }
public double total { get; set; }
}
然后您可以在GridView1_RowCommand
事件中添加代码。
private List<myclass> productDetails = new List<myclass>();
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "datacommand")
{
int productId = Convert.ToInt32(e.CommandArgument.ToString());
bool isexist = productDetails.Any(p => p.product_id == productId);
if (isexist)
{
myclass product = productDetails.Where(p => p.product_id == productId).FirstOrDefault();
productDetails.Add(new myclass
{
product_id = product.product_id,
price = product.price,
qty = product.qty + 1,
total = Convert.ToDouble((product.price) * (product.qty + 1))
});
}
else
{
double productPrice = Convert.ToDouble(GridView1.Rows[0].Cells[3].Text);
double productTotal = productPrice * 1;
productDetails.Add(new myclass
{
product_id = Convert.ToInt32(e.CommandArgument.ToString()),
price = productPrice,
qty = 1,
total = productTotal
});
}
}
}