Click here to Skip to main content
15,890,670 members
Please Sign up or sign in to vote.
1.44/5 (2 votes)
See more:
Hello,

How to update Total Amount in GridView Footer after any row deleted using c# asp.net?

Let's suppose Two row in my gridview and 200 in each row of amount column, so, when i am deleting one row, so, 600 is displaying in Total amount in footer.
but result should be 200 after 1 row deleted.

I am using this code for delete and update footer total:-

Total Amount Code:-

C#
private int totalAmount = 0;
protected void GvMyCart_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            totalAmount += Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "ProdCost"));
        }
        if (e.Row.RowType == DataControlRowType.Footer)
        {
            Label lblTotal = (Label)e.Row.FindControl("LblTotal");
            lblTotal.Text = totalAmount.ToString();
        }
    }

protected void GvMyCart_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        
            //var id = (((Label)GvMyCart.Rows[i].FindControl("lblProdID"))).Text;
            int id = Convert.ToInt32(GvMyCart.Rows[e.RowIndex].Cells[2].Text);
            SqlConnection con = new SqlConnection(connectionString);
            con.Open();
            SqlCommand cmd1 = new SqlCommand("procdeleteProd", con);
            cmd1.CommandType = CommandType.StoredProcedure;
            cmd1.Parameters.Add("@ProdID", SqlDbType.Int).Value = Convert.ToInt32(id);
            cmd1.ExecuteNonQuery();
            con.Close();
       
        BindGrid();
    }

private void BindGrid()
    {
        SqlConnection con = new SqlConnection(connectionString);
        con.Open();
        SqlCommand cmd1 = new SqlCommand("procMyCart", con);
        cmd1.CommandType = CommandType.StoredProcedure;
        SqlDataAdapter sda = new SqlDataAdapter(cmd1);
        DataTable dt = new DataTable();
        sda.Fill(dt);
        GvMyCart.DataSource = dt;
        GvMyCart.DataBind();
        con.Close();
    }


Please help me, My total amount did not update correctly.

Thanks in Advance.

Ankit Agarwal
Website Developer
Posted
Updated 30-Dec-13 3:49am
v2
Comments
Maciej Los 30-Dec-13 9:50am    
"My total amount did not update correctly" - is not informative at all ;(
Amey K Bhatkar 30-Dec-13 13:06pm    
Hi I think you are doing everything fine. Where you are calling the BindGrid function? Is it get called means deleted row is not visible inside the grid once you delete it right?

1 solution

Not sure where you have placed the total variable.

But you should have it inside the bind method to reassign so the rowdatabound will update the reset value like

C#
private void BindGrid()
    {
        totalAmount = 0;
        SqlConnection con = new SqlConnection(connectionString);
        con.Open();
        SqlCommand cmd1 = new SqlCommand("procMyCart", con);
        cmd1.CommandType = CommandType.StoredProcedure;
        SqlDataAdapter sda = new SqlDataAdapter(cmd1);
        DataTable dt = new DataTable();
        sda.Fill(dt);
        GvMyCart.DataSource = dt;
        GvMyCart.DataBind();
        con.Close();
    }


Try and revert.
 
Share this answer
 
v2

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