现在的位置: 首页 > 综合 > 正文

GridView实现多表头

2013年10月31日 ⁄ 综合 ⁄ 共 2378字 ⁄ 字号 评论关闭

天遇到了要合并GridView多表头实现的问题,上网上查了一下,总结了自己的方法,感觉也比较简单。话不多说,直接代码:

要写再GridView的RowCreated事件中:

protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
    ...{
        if (e.Row.RowType == DataControlRowType.Header)
        ...{
            TableCellCollection tcl = e.Row.Cells;
            //清除自动生成的表头
            tcl.Clear();
            //添加新的表头第一行
            tcl.Add(new TableHeaderCell());
            tcl[0].RowSpan = 3;
            tcl[0].Text = "污染物名称";
            tcl.Add(new TableHeaderCell());
            tcl[1].RowSpan = 3;
            tcl[1].Text = "产生量(吨)";
            tcl.Add(new TableHeaderCell());
            tcl[2].ColumnSpan = 4;
            tcl[2].Text = "去除量(吨)";
            tcl.Add(new TableHeaderCell());
            tcl[3].ColumnSpan = 9;
            tcl[3].Text = "排放量(吨)";
            tcl.Add(new TableHeaderCell());
            tcl[4].RowSpan = 3;
            tcl[4].Text = "污染物排放标准值</th></tr><tr>";
            //这里的</th></tr>表示是第一行结束,<tr>表示第二行开始,感觉就和table的表格合并一样
            //第二行表头
            tcl.Add(new TableHeaderCell());
            tcl[5].RowSpan = 2;
            tcl[5].Text = "合计";
            tcl.Add(new TableHeaderCell());
            tcl[6].RowSpan = 2;
            tcl[6].Text = "其中新增设施";
            tcl.Add(new TableHeaderCell());
            tcl[7].RowSpan = 2;
            tcl[7].Text = "其中燃烧废气";
            tcl.Add(new TableHeaderCell());
            tcl[8].RowSpan = 2;
            tcl[8].Text = "其中工艺废气";
            tcl.Add(new TableHeaderCell());
            tcl[9].ColumnSpan = 3;
            tcl[9].Text = "排放合计";
            tcl.Add(new TableHeaderCell());
            tcl[10].ColumnSpan = 3;
            tcl[10].Text = "其中燃烧废气排放量";
            tcl.Add(new TableHeaderCell());
            tcl[11].ColumnSpan = 3;
            tcl[11].Text = "其中工艺废气排放量</th></tr><tr>";
            //第三行表头 同上面所说的
            tcl.Add(new TableHeaderCell());
            tcl[12].Text = "合计";
            tcl.Add(new TableHeaderCell());
            tcl[13].Text = "达标量";
            tcl.Add(new TableHeaderCell());
            tcl[14].Text = "超标量";
            tcl.Add(new TableHeaderCell());
            tcl[15].Text = "合计";
            tcl.Add(new TableHeaderCell());
            tcl[16].Text = "达标量";
            tcl.Add(new TableHeaderCell());
            tcl[17].Text = "超标量";
            tcl.Add(new TableHeaderCell());
            tcl[18].Text = "合计";
            tcl.Add(new TableHeaderCell());
            tcl[19].Text = "达标量";
            tcl.Add(new TableHeaderCell());
            tcl[20].Text = "超标量";

        }
看太多没用,只要自己做了就知道,其实原来也很简单的!祝大家愉快!

 

抱歉!评论已关闭.