本文介绍了当我刷新网页时,如何防止出现重复的Gridview数据列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我创建了一个购物车网页应用程序。它运行良好。但问题是,当我在DataGridView.Since上添加项目到购物车时,GridView Rows是动态添加的。当我刷新包含DataGridView的购物车页面时,它会复制第一行。我用Google搜索了更多但我找不到。



这里我粘贴我的代码,请解决,如果有人有解决方案。

Hi,
I have created a shopping cart web application.It is working well as required.But problem is that when i add item to cart on DataGridView.Since,GridView Rows are dynamically added.When i refresh cart page having DataGridView, it duplicates first row.I googled more but i couldn't find.

here i paste my code,Please, resolve if somebody has solution.

protected void LinkButton1_Click(object sender, EventArgs e)
   {
       Button btn = (Button)sender;
       string strId = btn.CommandArgument.ToString();
       DataSet ds = GetProductDetailById(int.Parse(strId));
       DataTable dt = null; ;
       if (Session["CartItems"] != null)   //if datatable exists
       {

           dt = (DataTable)Session["CartItems"];
       }
       else
       {
           dt = new DataTable();  //creating new datatable
           dt.Columns.Add("ProducDtltId");
           dt.Columns.Add("itemName");
           dt.Columns.Add("FileName");
           dt.Columns.Add("quantity");
           dt.Columns.Add("UnitPrice");
       }
       DataRow dr = dt.NewRow();
       dr["ProducDtltId"] = strId;
       dr["itemName"] = ds.Tables[0].Rows[0]["itemName"].ToString();
       dr["FileName"] = ds.Tables[0].Rows[0]["FileName"].ToString();
       dr["quantity"] = 1;
       dr["UnitPrice"] = ds.Tables[0].Rows[0]["UnitPrice"].ToString();

       dt.Rows.Add(dr);//adding new row & assigning new session & id
       Session["CartItems"] = dt;
       FillCartDetails();
      }
   }
   public void FillCartDetails()
   {
       if (Session["CartItems"] != null)
       {
           DataTable dt = (DataTable)Session["CartItems"];
           grdCartDetail.DataSource = dt;
           grdCartDetail.DataBind();
           SetFooter();
       }
   }

推荐答案


Protected void gv_GridView_RowDataBound(object sender, GridViewRowEventArgs e)

{

       if (e.Row.RowType == DataControlRowType.DataRow)
        {

            if (e.Row.DataItemIndex != 0)
            {
                foreach(GridViewRow row in gv_GridView.Rows)
                {
                    if (e.Row.Cells[5].Text == row.Cells[5].Text)
                    {
                        e.Row.Cells[5].ForeColor = (e.Row.Cells[5].Text != row.Cells[5].Text) ? Color.Green : Color.Red;
                        MessageBox3.ShowError("You have entered the same person twice, please delete the second entry.");
                    }
                }

            }

        }

}


这篇关于当我刷新网页时,如何防止出现重复的Gridview数据列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-25 06:05