Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
3.50/5 (2 votes)
See more:
Hi. I'm working on shopping cart kind of application, in this I will be entering ProdID,Price,Quantity,Amount etc in to my Texboxes
after clicking on the Next button the list has to be added into gridview row for each product id.
now my problem is after entering 2 or 3 products it will reach certain amount limit(as customer may only have 500Rs).
so i may need to remove certain row to reduce the amount.
here to notice that after submit(not Next button)only data will get in to database.
so im trying to remove particular row from gridview list items in which event i should handle this(auto generate delte or rowcommand ..)
how to remove from gridview list.
private void BindGrid(int rowcount)
    {
        DataTable dt = new DataTable();
        DataRow dr;

        dt.Columns.Add(new System.Data.DataColumn("productId", typeof(String)));
        dt.Columns.Add(new System.Data.DataColumn("productName", typeof(String)));
        dt.Columns.Add(new System.Data.DataColumn("Price", typeof(String)));
        dt.Columns.Add(new System.Data.DataColumn("Quantity", typeof(int)));
        dt.Columns.Add(new System.Data.DataColumn("Amount", typeof(String)));
        //  dt.Columns.Add(new System.Data.DataColumn("TotalAmount", typeof(String)));
        

        if (ViewState["CurrentRow"] != null)
        {
            for (int i = 0; i < rowcount + 1; i++)
            {
                dt = (DataTable)ViewState["CurrentRow"];
                if (dt.Rows.Count > 0)
                {
                    dr = dt.NewRow();
                    dr[0] = dt.Rows[0][0].ToString();
                }
            }
            dr = dt.NewRow();
            dr[0] = TxtProductId.Text.ToString();
            dr[1] = TxtProductName.Text.ToString();
            dr[2] = decimal.Parse(Txtprice.Text.ToString());
            dr[3] = txtQuantity.Text.ToString();
            dr[4] = int.Parse(txtQuantity.Text.ToString()) * decimal.Parse(Txtprice.Text.ToString());
            dt.Rows.Add(dr);
            //dr = dt.NewRow();
            //dr[3] = "Total";
        }

-----------
private void BindGrid(int rowcount)
    {
        DataTable dt = new DataTable();
        DataRow dr;

        dt.Columns.Add(new System.Data.DataColumn("productId", typeof(String)));
        dt.Columns.Add(new System.Data.DataColumn("productName", typeof(String)));
        dt.Columns.Add(new System.Data.DataColumn("Price", typeof(String)));
        dt.Columns.Add(new System.Data.DataColumn("Quantity", typeof(int)));
        dt.Columns.Add(new System.Data.DataColumn("Amount", typeof(String)));
      

        if (ViewState["CurrentRow"] != null)
        {
            for (int i = 0; i < rowcount + 1; i++)
            {
                dt = (DataTable)ViewState["CurrentRow"];
                if (dt.Rows.Count > 0)
                {
                    dr = dt.NewRow();
                    dr[0] = dt.Rows[0][0].ToString();
                }
            }
            dr = dt.NewRow();
            dr[0] = TxtProductId.Text.ToString();
            dr[1] = TxtProductName.Text.ToString();
            dr[2] = decimal.Parse(Txtprice.Text.ToString());
            dr[3] = txtQuantity.Text.ToString();
            dr[4] = int.Parse(txtQuantity.Text.ToString()) * decimal.Parse(Txtprice.Text.ToString());
            dt.Rows.Add(dr);
        
        }

/////////////////// here i need in delete event. Help  i want to remove it from gridview item list before submiting data to DB ///////////////// 
 protected void Grd_sales_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
}
Posted
Updated 17-Mar-11 3:34am
v2
Comments
Amund Gjersøe 29-Mar-11 13:38pm    
Hard to figure out what you are thinking about when reading your question, but it seem like you want the user to add new items even when he is out of credits (or Rs). So instead of making the user choose what row to delete to be able to continue to use credits, you will delete a previously selected item automatically.

how to remove from gridview list.
You do this in the DataTable, not in the DataGridView. The only thing you do in a DataGridView is altering the presentation of the data, not the data them selves.
So get the row index in you event, find its equal in the datatable and copy that row to a variable (i.e. myRow), then call datatable.rows.remove(myRow), and the store myRow in the DB.
parmar_punit 16-May-11 8:06am    
i think you should try this...
gridView1.DeleteRow(rowIndex);
if you are working in RowDeleting event then use
gridView1.DeleteRow(e.RowIndex);

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