博客园  :: 首页  :: 新随笔  :: 联系 :: 管理

GridView加入自动求和求平均值小计?

Posted on 2008-03-08 00:26  匆匆  阅读(494)  评论(0编辑  收藏  举报
1.前台
要设置ShowFooter="True" ,否则默认表头为隐藏的!
2.后台
private double sum = 0;//取指定列的数据和
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
        
        if (e.Row.RowIndex >= 0)
        {
            sum += Convert.ToDouble(e.Row.Cells[6].Text);
        }
        else if (e.Row.RowType == DataControlRowType.Footer)
        {
            e.Row.Cells[5].Text = "总薪水为:";
            e.Row.Cells[6].Text = sum.ToString();
            e.Row.Cells[3].Text = "平均薪水为:";
            e.Row.Cells[4].Text = ((int)(sum / GridView1.Rows.Count)).ToString();
            
        }
}