当我刷新网页时,如何防止出现重复的Gridview数据列 [英] How Do I Prevent To Be Duplicate Gridview Data Column When I Refresh Web Page

查看:53
本文介绍了当我刷新网页时,如何防止出现重复的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();
       }
   }

推荐答案

之前

FillCartDetails() ;



Clear GridView



首先,null数据源:



this.dataGridView.DataSource = null;

然后清除行:



this.dataGridView.Rows .Clear();

然后将数据源设置为新列表:



this.dataGridView.DataSource = this.GetNewValues() ;
Before
FillCartDetails();

Clear GridView

Firstly, null the data source:

this.dataGridView.DataSource = null;
Then clear the rows:

this.dataGridView.Rows.Clear();
Then set the data source to the new list:

this.dataGridView.DataSource = this.GetNewValues();


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数据列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆