问题是这样的,一个GridView,我用2个FOR循环(调用存储过程)将值插入GridView的每一个单元格中
我现在想在每一行和每一列的最后一个单元格计算出这一行和这一列的和
请问在数据库中怎么实现?

解决方案 »

  1.   

    foreach(gridviewrow row in grd.rows)
    {
      int r=0;
      for (i=0;i<=5;i++) //假设有6列要统计到第七列
      {
        r+=int.parse(row.cells[i].text);
      } 
      row.cells[i].text=r.tostring();
    }
      

  2.   

    private double sum = 0;//取指定列的数据和,你要根据具体情况对待可能你要处理的是int
    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();
                
            }
        }
    前台:唯一的花头就是设置ShowFooter="True" ,否则默认表头为隐藏的!
      

  3.   

    数据库里
    聚集表达式 SUM
    再GROUP BY吧~