在gridview的header中添加新行

来源:互联网 发布:ffmpeg下载Linux 编辑:程序博客网 时间:2024/05/16 18:28

<asp:GridView id="GridView2" runat="server" Width="100%" AutoGenerateColumns="False"

ShowHeader="false" OnRowDataBound="GridView2_RowDataBound"

OnRowCreated="GridView2_RowCreated">
                       
                        <Columns >
                          
                         
                              <asp:BoundField DataField="name" HeaderText="Name"

SortExpression="name" >
                                                <ItemStyle HorizontalAlign="Left" />
                                                <HeaderStyle HorizontalAlign="Left" />
                                            </asp:BoundField>
                           <asp:BoundField DataField="address" HeaderText="Address"

SortExpression="address">
                                <ItemStyle HorizontalAlign="Left" />
                                <HeaderStyle HorizontalAlign="Left" />
                            </asp:BoundField>
<asp:BoundField DataField="phone" HeaderText="Phone" SortExpression="phone">
                                <ItemStyle HorizontalAlign="Left" />
                                <HeaderStyle HorizontalAlign="Left" />
                            </asp:BoundField>
  </Columns>
                    </asp:GridView>

 <table runat="server" id="tab_lab_def" visible="false">
                <tr>
                    <td style="height: 21px">
                    1</td>
                    <td style="height: 21px"> New line1
                    </td>
                    <td style="height: 21px">
                        First Name
                    </td>
                </tr>
                <tr>
                    <td>
                        2</td>
                    <td>
                       New line2 </td>
                    <td>
                       Last Name
                    </td>
                </tr>
 <tr>
                    <td>
                        3</td>
                    <td>
                       New line3 </td>
                    <td>
                       ALL Name
                    </td>
                </tr>
</table>

protected void Page_Load(object sender, EventArgs e)
    {
      
        if (!IsPostBack)
        {
           //显示GridView2 它会隐藏老header添加新3行header(从前台tab_lab_def中提取的内容

作为header)
           

        }
    }
 protected void GridView2_RowCreated(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.Header)
        {
            GridViewRow rowHeader = new GridViewRow(0, 0, DataControlRowType.Header,

DataControlRowState.Normal);

            TableHeaderCell cell;

            for (int i = 1; i < tab_lab_def.Rows.Count; i++)
            {
                cell = new TableHeaderCell();
                cell.HorizontalAlign = HorizontalAlign.Left;
                cell.Text = tab_lab_def.Rows[i].Cells[0].InnerText.Trim();
                rowHeader.Cells.Add(cell);
            }

            ((GridView)sender).Controls[0].Controls.AddAt(0, rowHeader);

            rowHeader = new GridViewRow(1, 1, DataControlRowType.Header,

DataControlRowState.Normal);

            for (int j = 1; j < tab_lab_def.Rows.Count; j++)
            {
                cell = new TableHeaderCell();
                cell.HorizontalAlign = HorizontalAlign.Left;
                cell.Text = tab_lab_def.Rows[j].Cells[1].InnerText.Trim();
                rowHeader.Cells.Add(cell);
            }

            ((GridView)sender).Controls[0].Controls.AddAt(1, rowHeader);

            rowHeader = new GridViewRow(2, 2, DataControlRowType.Header,

DataControlRowState.Normal);

            for (int k = 1; k < tab_lab_def.Rows.Count; k++)
            {
                cell = new TableHeaderCell();
                cell.HorizontalAlign = HorizontalAlign.Left;
                cell.Text = tab_lab_def.Rows[k].Cells[2].InnerText.Trim();
                rowHeader.Cells.Add(cell);
            }

            ((GridView)sender).Controls[0].Controls.AddAt(2, rowHeader);
        }
    }

原创粉丝点击