GridView的多表头加排序同时实现

在月儿的博客上看到了GridView的多表头的用法,但是把GV自带的排序功能弄没了,我把代码改了下,同时实现这两个功能,很不错的

后台代码

    protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
    {
        //创建多表头
        if (e.Row.RowType == DataControlRowType.Header)
        {
            int x = 0;//创建个变量直接把cell插入到表头而非清空从写就能保存原来的功能了
            TableCellCollection tcHeader = e.Row.Cells;
            //第一列
            tcHeader.AddAt(x,new TableHeaderCell());

            tcHeader[x].Attributes.Add("colspan", "5"); //跨Column
            //tcHeader[0].Attributes.Add("rowspan", "2"); //跨row
            tcHeader[x].Text = "</th></tr><tr> ";//输入html换行实现多表头
            x++;
            .....
            //第二列 只添加css属性调整
            tcHeader[x].Attributes.Add("style", "background-color:LightSteelBlue");
            x++;
        }
    }

你可能感兴趣的:(GridView)