ASPxGridView控件实现多表头的两种方法


ASPxGridView控件实现多表头的两种方法

本文来源于:http://www.lmwlove.com/ac/ID490

 


在项目中一直都是用ASPxGridView控件,免不了需要在ASPxGridView控件上实现多表头,本人研究了实现多表头的两种方法,以供大家参考。

因ASPxGridView与GridView控件差不多,以下思路同样也适用于微软的GridView控件。

以下代码中表头的细节处理为本人项目所需,读者可根据自己的需求修改代码。

方法一:在Render事件中重写AspxGridView表头,主要思路是获取到表头对象,再重绘表头。
 
     protected override void Render(HtmlTextWriter writer)
        {
            GridViewHtmlTable table = (GridViewHtmlTable)this.grid.FindControl("DXHeaderTable");
            table.Rows.Clear();

            TableRow tr = new TableRow(); //第一行
            TableCell tc;
            for (int i = 0; i < 18; i++)
            {
                tc = new TableCell();
//自己的tc处理逻辑

                tr.Cells.Add(tc);
            }
            table.Rows.AddAt(0, tr);

            tr = new TableRow(); //第二行
            for (int i = 1; i <= 9; i++)
            {
                tc = new TableCell();
//自己的tc处理逻辑

                tr.Cells.Add(tc);
            }
            table.Rows.AddAt(1, tr);

            base.Render(writer);
        }


方法二:在AspxGridView的行创建事件中重绘表头,主要思路是在创建表头时就根据自己的需要创建。
 
       protected void grid_HtmlRowCreated(object sender, ASPxGridViewTableRowEventArgs e)
        {
            if (e.RowType == GridViewRowType.Data && e.VisibleIndex == grid.PageIndex * grid.SettingsPager.PageSize)
            {
                Table table = e.Row.Parent as Table;
                table.Rows.RemoveAt(0);

                TableRow tr = new TableRow(); //第一行
                TableCell td;

                for (int i = 0; i < 8; i++)
                {
                    tc = new TableCell();
    //自己的tc处理逻辑

                    tr.Cells.Add(tc);
                }

                table.Rows.AddAt(0, tr);

                tr = new TableRow(); //第二行
                for (int i = 0; i < 6; i++)
                {
                    tc = new TableCell();
    //自己的tc处理逻辑

                    tr.Cells.Add(tc);
                }
                table.Rows.AddAt(1, tr);
            }
        }

本文来源于:http://www.lmwlove.com/ac/ID490

posted @ 2013-06-04 08:58  Daniel_Lu  阅读(429)  评论(0编辑  收藏  举报