如何在使用c#asp.net删除任何行后更新GridView页脚中的总金额? [英] How to update Total Amount in GridView Footer after any row deleted using c# asp.net?

查看:63
本文介绍了如何在使用c#asp.net删除任何行后更新GridView页脚中的总金额?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,



如何在使用c#asp.net删除任何行后更新GridView页脚中的总金额?



假设我的gridview中有两行,amount列的每一行中有200行,因此,当我删除一行时,因此,600以页脚的总金额显示。

但是删除1行后结果应为200.



我使用此代码删除和更新页脚总数: -



总金额代码: -



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:-

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();
    }





请帮帮我,我的总金额没有正确更新。



先谢谢。



Ankit Agarwal

网站开发者



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

Thanks in Advance.

Ankit Agarwal
Website Developer

推荐答案

不确定放置总变量的位置。



但你应该在bind方法中重新分配所以rowdatabound会更新重置值,如



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

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.


这篇关于如何在使用c#asp.net删除任何行后更新GridView页脚中的总金额?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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