使用 GridView 生成数据表
本文关键字:数据表 GridView 使用 | 更新日期: 2023-09-27 18:33:20
我有网格视图,在该网格视图中我有一个按钮,在选择该按钮时,我需要在数据表中插入/更新记录。如果在数据表中,如果值存在,则 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());
}
}
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;
}
}
}
}
所以这就是答案
首先为属性创建类,您可以修改此设置,然后使用。
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
});
}
}
}