在gridview中合并合并标题列?

时间:2010-11-16 04:21:12

标签: c# asp.net

如何使用合并合并标题列创建以下类型的gridview?如果有人与我分享例子。

alt text

提前致谢。

1 个答案:

答案 0 :(得分:3)

GridView代码

<asp:GridView ID="grvMergeHeader" runat="server"
              BackColor="LightGoldenrodYellow"
              BorderColor="Tan" BorderWidth="5px"
              CellPadding="3" ForeColor="Black"
              GridLines="None" BorderStyle="None"
              CellSpacing="2"
              AutoGenerateColumns="False"
              DataSourceID="SqlDataSource1"
              OnRowCreated="grvMergeHeader_RowCreated">
    <FooterStyle BackColor="Tan" />
    <SelectedRowStyle BackColor="DarkSlateBlue"
                      ForeColor="GhostWhite" />
    <PagerStyle BackColor="PaleGoldenrod"
                ForeColor="DarkSlateBlue"
                HorizontalAlign="Center" />
    <HeaderStyle BackColor="Tan" Font-Bold="True" />
    <AlternatingRowStyle BackColor="PaleGoldenrod" />
    <Columns>
        <asp:BoundField DataField="DepartMentID"
                        HeaderText="DepartMentID"
                        SortExpression="DepartMentID" />
        <asp:BoundField DataField="DepartMent"
                        HeaderText="DepartMent"
                        SortExpression="DepartMent" />
        <asp:BoundField DataField="Name"
                        HeaderText="Name"
                        SortExpression="Name" />
        <asp:BoundField DataField="Location"
                        HeaderText="Location"
                        SortExpression="Location" />
    </Columns>
</asp:GridView>

CS代码

//row created
protected void grvMergeHeader_RowCreated(object sender, GridViewRowEventArgs e) {
    if (e.Row.RowType == DataControlRowType.Header) {
        GridView HeaderGrid = (GridView)sender;
        GridViewRow HeaderGridRow = new GridViewRow(0, 0, DataControlRowType.Header, 
                                                    DataControlRowState.Insert);  //creating new Header Type 
        TableCell HeaderCell = new TableCell(); //creating HeaderCell
        HeaderCell.Text = "Department";
        HeaderCell.ColumnSpan = 2;
        HeaderGridRow.Cells.Add(HeaderCell);//Adding HeaderCell to header.

        HeaderCell = new TableCell();
        HeaderCell.Text = "Employee";
        HeaderCell.ColumnSpan = 2;
        HeaderGridRow.Cells.Add(HeaderCell);

        grvMergeHeader.Controls[0].Controls.AddAt(0, HeaderGridRow);
    }
}

我希望这对你有用。