GridView的多表头(排序)实例方法

在多表头中实现如下

的多表头实现思路:

 

第一:在页面编写界面代码。

第二:在后台实现逻辑编码。

第三:出来显现方式的样式。

编码实现:

  第一界面编码:

 <table cellpadding="0" cellspacing="0" class="coolitePanleHeader" width="780px">
        <tr class="coolitePanleContent">
                <td>
                 <asp:GridView ID="grdRptPowerSafety" UseAccessibleHeader="false" runat="server" 
        AutoGenerateColumns="false" AllowSorting="true" 
                  OnRowCreated="grdRptPowerSafety_RowCreated" onsorting="grdRptPowerSafety_Sorting">
                        <Columns>
                       <asp:BoundField HeaderText="上级企业名称" runat="server"  DataField="pname" Visible="false" 
           ShowHeader="true"  ItemStyle-Width="126px"></asp:BoundField>
                            <asp:BoundField HeaderText="企业名称" runat="server"  DataField="dname" ShowHeader="true" 
             ItemStyle-Width="126px"></asp:BoundField>

                            <asp:BoundField DataField="c04"  ShowHeader="true"   ItemStyle-Width="80px"/>
                            <asp:BoundField DataField="name" ShowHeader="true"  ItemStyle-Width="120px"/>
                            <asp:BoundField DataField="c02"  ShowHeader="true"  ItemStyle-Width="60px"/>
                            <asp:BoundField DataField="f01"   HeaderText="本月" ItemStyle-Width="40px" SortExpression="f01"/>
                            <asp:BoundField DataField="f02" HeaderText="累计"  ItemStyle-Width="40px" SortExpression="f02"/>
                            <asp:BoundField DataField="f03" HeaderText="当月"  ItemStyle-Width="40px" SortExpression="f03"/>
                            <asp:BoundField DataField="f04" HeaderText="累计"  ItemStyle-Width="40px" SortExpression="f04"/>
                            <asp:BoundField DataField="f05" HeaderText="本月"  ItemStyle-Width="40px" SortExpression="f05"/>
                            <asp:BoundField DataField="f06" HeaderText="累计"  ItemStyle-Width="40px" SortExpression="f06"/>
                            <asp:BoundField DataField="c03" HeaderText="责任单位"  ItemStyle-Width="100px" SortExpression="c03"/>
                            <asp:BoundField DataField="c05" HeaderText="备注"  ItemStyle-Width="100px"/>

                        </Columns>
                        <FooterStyle CssClass="GridViewFooterStyle" />
                        <RowStyle CssClass="GridViewRowStyle" />
                        <SelectedRowStyle CssClass="GridViewSelectedRowStyle" />
                        <PagerStyle CssClass="GridViewPagerStyle" />
                        <AlternatingRowStyle CssClass="GridViewAlternatingRowStyle" />
                        <HeaderStyle CssClass="GridViewHeaderStyleHeaders" />
                    </asp:GridView>   
                </td>
        </tr>
 </table> 
第二:后台代码实现
  protected void grdRptPowerSafety_RowCreated(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.Header)
        {
            TableCellCollection tcc = e.Row.Cells;
            ////tcc.Clear();
            int intcount = tcc.Count;
            //tcc.Add(new TableHeaderCell());
            tcc[0].Attributes.Add("rowspan", "2");
            tcc[1].Attributes.Add("rowspan", "2");
           // tcc[0].Text = "企业名称</th>";

            tcc.Add(new TableHeaderCell());
            tcc[13].Attributes.Add("colspan", "3");
            tcc[13].Text = "项目</th>";

           tcc.Add(new TableHeaderCell());
            tcc[14].Attributes.Add("colspan", "2");
            tcc[14].Text = "本年</th>";

           tcc.Add(new TableHeaderCell());
            tcc[15].Attributes.Add("colspan", "2");
            tcc[15].Text = "上年同期</th>";

            tcc.Add(new TableHeaderCell());
            tcc[16].Attributes.Add("colspan", "2");
            tcc[16].Text = "同比(数量)</th>";

            tcc.Add(new TableHeaderCell());
            tcc[17].Attributes.Add("colspan", "2");
            tcc[17].Text = "说明</th></tr><tr>";

            //tcc[6].Attributes.Add("colspan", "3");
            //tcc[6].Text = "说明1</th>";
            //tcc[7].Text = "说明2</th>";
            //tcc[8].Text = "说明3</th>";
            for (int i = 2; i < intcount; i++)
            {
                tcc.Add(tcc[2]);
            }
}

第三:扩张方式在 for (int i = 2; i < intcount; i++)       

     {              

  tcc.Add(tcc[2]);         

   }

中以I为准:如果前面实现以合并2行的情况下是从第二个开始的堆栈。

posted @ 2010-11-29 11:03  lcisok  阅读(1551)  评论(0编辑  收藏  举报