1. 程式人生 > >GridView自動求和、求平均值小計

GridView自動求和、求平均值小計

轉自清清月兒gridview72絕技,略有改動

前臺:唯一的花頭就是設定ShowFooter="True"

private double sum = 0;//取指定列的資料和,你要根據具體情況對待可能你要處理的是int
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            sum += Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "amount").ToString());

        }

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