ASP.NET GridView中的多列标题

时间:2016-03-29 18:18:24

标签: c# asp.net .net gridview

我有一个gridview包含30列,其中几列我必须在它上面设置单列,即我有Leaf Name,Leaf Code,Leaf Value,Leaf Vendor Code,Leaf Id,Leaf ZOnal,Leaf Leveller列,我需要在其上面给出单个列Leaves Details。我在rowdatabound中执行了以下代码但没有成功,新的单列是最重要的。请你告诉我我做错了什么。我做的更重要的是,我设置gridview属性PagerSettings Position top这样就可以了作为指数值1,2,3等来到网格顶部。

这是我的gridview

 <asp:GridView ID="grdcell" runat="server" AutoGenerateColumns="False" AllowPaging="true" PageSize="13" DataKeyNames="firstname,location"
                CellPadding="4" ForeColor="#333333" OnPageIndexChanging="grdNewcells_PageIndexChanging" OnRowDataBound="grdcell_RowDataBound" >
                <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
                <Columns>
                    <asp:TemplateField HeaderText="Insert" HeaderStyle-HorizontalAlign="Left">
                        <HeaderTemplate>
                            <asp:CheckBox ID="insertall" Text="Insert All" onclick="checkAll(this);" runat="server" />
                        </HeaderTemplate>
                        <ItemTemplate>
                            <asp:CheckBox ID="insertChk" runat="server" />
                        </ItemTemplate>
                        <HeaderStyle HorizontalAlign="Left"></HeaderStyle>
                    </asp:TemplateField>
                    <asp:BoundField DataField="Leaf_NAME" HeaderText="Leaf Name" SortExpression="Leaf_NAME"></asp:BoundField>
                    <asp:BoundField DataField="Leaf_CODE" HeaderText="Leaf Code" SortExpression="Leaf_CODE"></asp:BoundField>
                    <asp:BoundField DataField="Leaf_Value" HeaderText="Leaf Value" SortExpression="Leaf_Value"></asp:BoundField>
                    <asp:BoundField DataField="LeafVENDOR_CODE" HeaderText="Leaf Vendor Code" SortExpression="LeafVENDOR_CODE"></asp:BoundField>
                    <asp:BoundField DataField="Leaf_ID" HeaderText="Leaf Id" SortExpression="Leaf_ID"></asp:BoundField>
                    <asp:BoundField DataField="Leaf_ZOnal" HeaderText="Leaf ZOnal" SortExpression="Leaf_ZOnal"></asp:BoundField>
                    <asp:BoundField DataField="Leaf_Leveller" HeaderText="Leaf Leveller" SortExpression="Leaf_Leveller"></asp:BoundField>
                    <asp:BoundField DataField="Loaction" HeaderText="Loaction" SortExpression="Loaction"></asp:BoundField>

以及我在row data bound中所做的代码,如下所示

GridViewRow gvRow = e.Row;
                if (gvRow.RowType == DataControlRowType.Header)
                {
                    if (gvRow.Cells[1].Text == "Leaf_NAME")
                    {
                        gvRow.Cells.Remove(gvRow.Cells[1]);
                        GridViewRow gvHeader = new GridViewRow(0, 0, DataControlRowType.DataRow, DataControlRowState.Insert);
                        TableCell headerCell0 = new TableCell()
                        {
                            Text = "Leaves Details",
                            HorizontalAlign = HorizontalAlign.Center,
                            RowSpan = 6
                        };

                        gvHeader.Cells.Add(headerCell0);

                        grdcell.Controls[0].Controls.AddAt(0, gvHeader);
                    }
                }

2 个答案:

答案 0 :(得分:2)

我是通过使用以下链接完成的,但我使用了gridview行创建的事件

single column for multiple column

答案 1 :(得分:0)

为了在公共标题(带子标题)中“分组”多个列,我使用了TemplateField。对于2个子列,它看起来像这样:

<asp:TemplateField HeaderStyle-HorizontalAlign="Center">
    <HeaderTemplate>
        <table runat="server" class="leafCommonHeader" align="center" cellpadding="0" cellspacing="0">
            <tr class="multiColumnMainHeader">
                <td colspan="2">
                    <asp:Label runat="server" CssClass="leafHeader" Text="Leaf Details" />
                </td>
            </tr>
            <tr runat="server">
                <td class="leafNameField">
                    <asp:LinkButton runat="server" CssClass="leafHeader" OnCommand="gvLeavesColumnSort" CommandArgument="LeafName" Text="Name" />
                </td>
                <td class="leafNameField">
                    <asp:LinkButton runat="server" CssClass="leafHeader" OnCommand="gvLeavesColumnSort" CommandArgument="LeafCode" Text="Code" />
                </td>
            </tr>
        </table>
    </HeaderTemplate>
    <ItemTemplate>
        <table runat="server" class="leafCommonItem" cellpadding="0" cellspacing="0">
            <tr>
                <td class="leafNameField">
                    <asp:Literal runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "LeafName")) %>' />
                </td>
                <td class="leafCodeField">
                    <asp:Literal runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "LeafCode") %>' />
                </td>
            </tr>
        </table>
    </ItemTemplate>
</asp:TemplateField>

子标题中的链接按钮用于实现子列的排序。我添加了一些允许自定义单元格和标签的CssClass属性。例如,CSS类leafNameFieldleafCodeField可以设置其列的内容。

免责声明:此代码未在启用分页时进行测试。