嵌套gridview C#

时间:2011-10-21 15:18:22

标签: c# asp.net

我的页面中有一个嵌套的gridview。第一个用作标题,因此这将显示段名称。第二个用于显示细分细节。一切都很好,除了我在设计第一个网格时遇到问题。我想在这个页面中显示数据的方式是 细分(换行) 细节

但它在一行中显示为细分细节。如何使网格显示在占据页面整个宽度的两行中。以下是它目前的设置方式。

<asp:GridView ID="gvSegments" runat="server" AutoGenerateColumns="false" GridLines="None" Width="900px" OnRowDataBound="gvSegments_RowDataBound">
    <Columns>
        <asp:TemplateField ShowHeader="false">
            <ControlStyle Width="140px" />
            <ItemTemplate>
                <asp:HiddenField ID="hfSegmentId" runat="server" Value='<%# Bind("SegmentId") %>' />
                <asp:Label ID="lblSegmentDesc" runat="server" Text='<%# Bind("SegmentDesc") %>' Font-Bold="true" />
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField ShowHeader="false">
            <ItemTemplate>
                <asp:GridView ID="gvSegmentDetails" runat="server" AutoGenerateColumns="false" GridLines="None" OnRowDataBound="gvSegmentDetails_RowDataBound" Font-Size="11px">
                    <Columns>
                        <asp:TemplateField ShowHeader="false">
                            <ItemStyle HorizontalAlign="Center" />
                            <ItemTemplate>
                                <asp:CheckBox ID="chkAction" runat="server" />
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Medium">
                            <ControlStyle Font-Size="11px" />
                            <ItemStyle HorizontalAlign="Center" />
                            <ItemTemplate>
                                 <asp:HiddenField ID="hfSegmentId" runat="server" Value='<%# Bind("SegmentId") %>' />
                                 <asp:HiddenField ID="hfSegmentDetailId" runat="server" Value='<%# Bind("SegmentDetailId") %>' />
                                 <asp:Label ID="lblMediaType" runat="server" Text='<%# Bind("MediaTypeDesc") %>' />
                             </ItemTemplate>
                         </asp:TemplateField>
                         <asp:TemplateField HeaderText="Cost per Piece">
                             <ItemStyle HorizontalAlign="Center" />
                             <ItemTemplate>
                                 <asp:Label ID="lblCostPerPiece" runat="server" Text='<%# Bind("CostPerPiece") %>' />
                             </ItemTemplate>
                         </asp:TemplateField>
                         <asp:TemplateField ShowHeader="false">
                             <ItemTemplate>
                                 <asp:LinkButton ID="lnkEditMedia" runat="server" Text="Details" OnClick="lnkEditMedia_Click" />
                             </ItemTemplate>
                         </asp:TemplateField>
                         <asp:TemplateField HeaderText="Advertising Months">
                             <ControlStyle Font-Size="11px" />
                             <ItemStyle HorizontalAlign="Center" />
                             <ItemTemplate>
                                 <asp:Label ID="lblAdvertisingMonths" runat="server" Text='<%# Bind("Months") %>' />
                             </ItemTemplate>
                         </asp:TemplateField>
                         <asp:TemplateField HeaderText="Budget">
                             <ControlStyle Width="70px" Font-Size="11px" />
                             <ItemStyle HorizontalAlign="Center" />
                             <ItemTemplate>
                                 <asp:Label ID="txtBudget" runat="server" Text='<%# Bind("DropAmount", "{0:$###,###,##0.00}") %>' />
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Total Quantity">
                            <ItemStyle HorizontalAlign="Center" />
                            <ControlStyle Width="50px" Font-Size="11px" />
                            <ItemTemplate>
                                <asp:Label ID="lblTotalQty" runat="server" Text='<%# Bind("DropQuantity") %>' />
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Select Creative">
                            <ControlStyle Font-Size="11px" />
                            <ItemTemplate>
                                <asp:DropDownList ID="ddrCreative" runat="server" AppendDataBoundItems="true" OnSelectedIndexChanged="ddrCreative_SelectedChanged" AutoPostBack="true">
                                    <asp:ListItem Value="-1" Text="Select One" Selected="True" />
                                </asp:DropDownList>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField ShowHeader="false">
                            <ControlStyle Font-Size="11px" />
                            <ItemTemplate>
                                <asp:HiddenField ID="hfCreativeUrl" runat="server" />
                                <asp:HyperLink ID="hprCreativeUrl" runat="server" Target="_blank" Text="Click here" />
                            </ItemTemplate>
                        </asp:TemplateField>
                    </Columns>
                    <HeaderStyle BackColor="#a73e20" ForeColor="White" />
                    <AlternatingRowStyle BackColor="#E4E4E4" />
                    <RowStyle BackColor="#F7F7F7" />
                </asp:GridView>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

1 个答案:

答案 0 :(得分:1)

一种方法(尽管可能不是你想要的)是将嵌套的GridView放在与你的第一列相同的ItemTemplate中,并带有中断和/或水平线或类似的东西。它可能看起来像:

<asp:GridView ID="gvSegments" runat="server">
<Columns>
    <asp:TemplateField>
        <ItemTemplate>
            <asp:HiddenField />
            <asp:Label />

            <br />
            <hr />

            <asp:GridView />
        </ItemTemplate>
    </asp:TemplateField>
</Columns>
</asp:GridView>

然后,要使行跨越整个窗口,您只需将ItemStyle添加到TemplateField

即可
<asp:TemplateField ItemStyle-Width="100%">