注:将ShowFooter属性设置为true

       int mysum1 = 0;
        Double mysum2 = 0;
        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                DataRowView myrows = (DataRowView)e.Row.DataItem;

                  //要统计总数的列
                mysum1 += Convert.ToInt32(myrows[8].ToString());
                mysum2 += Convert.ToDouble(myrows[9].ToString());
            }
            // 合计
            //if (e.Row.RowType == DataControlRowType.Footer)
            //{
            //    e.Row.Cells[0].Text = "合计";
            //    e.Row.Cells[8].Text = "总本数:" + mysum1.ToString();
            //    e.Row.Cells[9].Text =mysum2.ToString();
            //}
        }

      //合并单元格

        protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
        {

          //判断是否是表脚
            if (e.Row.RowType == DataControlRowType.Footer)
            {

                //获取表脚所在的所有单元格
                TableCellCollection TableCell = e.Row.Cells;

              //自动清除表脚
                TableCell.Clear();
                TableCell.Add( new TableHeaderCell() );
                TableCell[0].Text = "合计:";

                TableCell.Add(new TableHeaderCell());
                TableCell[1].ColumnSpan =8;                          //合并行,要和并列是需设置RowSpan  ;从第二个单元格一次后合并8个
                TableCell[1].Text = "书本数总计:" + mysum1.ToString()+"本";

                TableCell.Add(new TableHeaderCell());
                TableCell[2].ColumnSpan = 2;
                TableCell[2].Text = "实样总计:" + mysum2.ToString()+"元";
            }
        }

posted on 2011-02-18 15:56  高兴happy  阅读(178)  评论(0编辑  收藏  举报