Gridview添加合计行问题,浪费了2小时,写出来共享一下

首先设置 showfooter=true;
在首页定义两个变量:  public float Mysum1;    public float Mysum2;

然后写 RowDatabind方法:
 protected void GridView1_RowCreated(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            base.GridView_RowDataBound(e, 1, 18);
            base.GridView_RowDataBound(e, 9, 28);
             DataKey Keys = GridView1.DataKeys[e.Row.RowIndex];
             TableCell myTableCell;
             myTableCell = e.Row.Cells[5];
             HyperLink LB = (HyperLink)(myTableCell.Controls[0]);
             LB.Enabled = Keys["SZLX"].ToString().Trim() == "0";
            DataRowView  MyRows=(DataRowView)e.Row.DataItem;
            Mysum1 += MyRows["SR"] is DBNull ? 0 : float.Parse(MyRows["SR"].ToString());
            Mysum2 += MyRows["ZC"] is DBNull ? 0 : float.Parse(MyRows["ZC"].ToString());
/* 注意这里MyRows["SR"]网上的例子用的是索引号MyRows[6],这是假定你所显示的字段顺序和数据源的顺序是一至的,事实上我们都 是用select* from 这样的语句,所以用序号会出现错误,所以这个地方应该用字段用而不是索引号 ,出现数据类型转换错误我觉得可能就是这个原因造成的*/


        }
       

        if (e.Row.RowType == DataControlRowType.Footer)
        {
            e.Row.Cells[0].Text = "合计:";
            e.Row.Cells[6].Text = string.Format("{0:f2}", Mysum1);
            e.Row.Cells[7].Text = string.Format("{0:f2}", Mysum2);
        }

    }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值