【问题标题】:Generating Datatable using GridView使用 GridView 生成数据表
【发布时间】:2013-09-30 05:14:55
【问题描述】:

我有网格视图,在那个网格视图中我有一个按钮,在选择该按钮时,我需要在数据表中插入/更新记录。如果在数据表中存在值,则 qty 字段将增加一。否则将在数据表中插入数量为 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());
    }

}

【问题讨论】:

  • 你在Gridview设计任务中添加了命令域编辑、更新和取消吗?
  • 不...只是“AddtoCart”按钮...我需要这个。
  • 如果你想编辑和更新你的gridview你需要在gridview设计任务中添加命令字段
  • 我需要更新“DataTable”中的值。不在网格视图中。我在我的问题中明确提到了这一点。
  • 试试通用列表。在您可以轻松地对此进行查询之后。

标签: c# asp.net gridview datatable


【解决方案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;
        }
     }
  }
}

这就是答案

【讨论】:

  • 它进入 if(aa=="dup") 的 for 循环,但该循环中的条件不匹配。所以它不是增加数量的价值。
【解决方案2】:

首先为属性创建类, 你可以修改这个然后使用。

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
                    });
                }

        }
}

【讨论】:

  • 创建列表对象private List&lt;myclass&gt; productDetails = new List&lt;myclass&gt;();
  • 它没有进入条件 'if (productDetails != null && productDetails.Count() > 0)' 因为 procedureDetails 不包含任何值。
  • 它在我身边不起作用。我尝试了第二个答案,我得到了正确的结果。无论如何,感谢您对我的帮助。
猜你喜欢
  • 1970-01-01
  • 2011-05-08
  • 2017-09-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-29
  • 2014-01-01
相关资源
最近更新 更多