Click here to Skip to main content
15,886,798 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.
C#
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();
       }
   }
Posted
Updated 4-Oct-17 5:50am
v2

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();
 
Share this answer
 
Comments
Member 10296662 6-Apr-15 6:31am    
sorry to be late....
but it works as suggested with some chages ....

thanks....sir.
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.");
                    }
                }

            }

        }

}
 
Share this answer
 
Comments
Richard Deeming 5-Oct-17 13:43pm    
This question was asked, answered, and solved TWO AND A HALF YEARS AGO.

Your "solution" makes no attempt to answer the question. And your code WILL NOT WORK in an ASP.NET application.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900