外网格视图具有嵌套网格视图的额外列

时间:2014-07-09 22:13:40

标签: c# asp.net

我在gridview中有嵌套gridview的以下代码,但它正在为外gridview标题和行生成一个额外的空白列,如下所示。我使用的代码如下。有没有办法删除外部gridview中的额外空白列。

在嵌套网格视图之前添加以下代码:

<asp:Literal runat="server" ID="lit1" Text="<tr id='trGrid'><td colspan='4'>" />

在嵌套网格视图之后添加以下代码:

<asp:Literal runat="server" ID="lit2" Text="</td></tr>" />

Outer gridview with nested gridview below outer gridview row

更新

如果我将colspan更改为3,则如下所示:

enter image description here

更新(整个网格代码):

<asp:GridView ID="GrdXML" runat="server" AllowSorting="false"
    AutoGenerateColumns="False" CellPadding="0"
    ForeColor="#333333" GridLines="Both" onrowdeleting="GrdXML_RowDeleting" OnRowDataBound="OnRowDataBound"
    ShowFooter="false" CssClass="xmlgrid" OnSorting="GrdXML_Sorting" DataKeyNames="ContractRecordID">
    <Columns>
        <asp:TemplateField HeaderText="Contract Start Date" SortExpression="ContractStartDate">
            <ItemTemplate>
                <asp:Label ID="Label2" runat="server" Text='<%# Bind("ContractStartDate") %>'></asp:Label>
            </ItemTemplate>
            <ItemStyle Width="30%" />
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Contract End Date" SortExpression="ContractEndDate">
            <ItemTemplate>
                <asp:Label ID="Label3" runat="server" Text='<%# Bind("ContractEndDate") %>'></asp:Label>
            </ItemTemplate>
            <ItemStyle Width="50%" />
        </asp:TemplateField>
        <asp:TemplateField>
            <ItemTemplate>
                <asp:Button ID="BtnDelete" runat="server" 
                    CommandArgument='<%# Eval("ContractRecordID") %>' CommandName="Delete" 
                    onclick="BtnDelete_Click" Text="Delete" Width="60px" />
                <ajaxToolkit:ConfirmButtonExtender ID="BtnDelete_ConfirmButtonExtender" 
                    runat="server" ConfirmText="Are you sure you want to Delete?" Enabled="True" 
                    TargetControlID="BtnDelete">
                </ajaxToolkit:ConfirmButtonExtender>
            </ItemTemplate>
            <ItemStyle Width="40px" />
        </asp:TemplateField>
        <asp:TemplateField>
            <ItemTemplate>
                <asp:Literal runat="server" ID="lit1" Text="<tr id='trGrid'><td colspan='3'>" />
                <asp:GridView ID="projectCodeGridView" runat="server" AllowSorting="false" CellPadding="4" ForeColor="#333333" GridLines="Horizontal" AutoGenerateColumns="false" ShowFooter="true" OnRowCommand="ProjectCodeGridView_RowCommand" OnRowCreated="ProjectCodeGridView_RowCreated" OnRowDataBound="ProjectCodeGridView_RowDataBound" OnRowDeleting="ProjectCodeGridView_RowDeleting" DataKeyNames="ContractProjectCodeID" BorderStyle="None" BorderWidth="0">
                    <Columns>
                        <asp:TemplateField HeaderText="Project Codes" SortExpression="ContractProjectCode" HeaderStyle-HorizontalAlign="Left">
                            <ItemTemplate>
                                <asp:Label ID="Label1" runat="server" Text='<%# Bind("ContractProjectCode") %>'></asp:Label>
                            </ItemTemplate>
                            <ItemStyle Width="100%" />
                            <FooterTemplate>
                                <asp:TextBox ID="projectCodeTextBox" runat="server"></asp:TextBox>
                                <asp:RequiredFieldValidator ID="projectCodeRequiredFieldValidator" runat="server" 
            ControlToValidate="projectCodeTextBox" ErrorMessage="Project Code Required" ForeColor="Red" Display="Static"></asp:RequiredFieldValidator>
                            </FooterTemplate>
                            <FooterStyle Width="100%" />
                        </asp:TemplateField>
                        <asp:TemplateField>
                            <ItemTemplate>
                                <asp:Button ID="BtnDelete1" runat="server" 
                    CommandArgument='<%# Eval("ContractProjectCodeID") %>' CommandName="Delete" 
                    onclick="BtnDelete1_Click" Text="Delete" Width="60px" />
                <ajaxToolkit:ConfirmButtonExtender ID="BtnDelete1_ConfirmButtonExtender" 
                    runat="server" ConfirmText="Are you sure you want to Delete?" Enabled="True" 
                    TargetControlID="BtnDelete1">
                </ajaxToolkit:ConfirmButtonExtender>
                            </ItemTemplate>
                            <FooterTemplate>
                                <asp:Button ID="BtnAdd" runat="server" onclick="BtnAdd_Click" Text="Add" Width="60px" />
                            </FooterTemplate>
                            <FooterStyle Width="100%" />
                        </asp:TemplateField>
                    </Columns>
                    <HeaderStyle BackColor="#F7F6F3" Font-Bold="True" />
                </asp:GridView>
                <asp:Literal runat="server" ID="lit2" Text="</td></tr>" />
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
    <EditRowStyle BackColor="#999999" />
    <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
    <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="Black" CssClass="padding" />
    <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
    <RowStyle BackColor="#F7F6F3" ForeColor="#333333" HorizontalAlign="Center" CssClass="padding" />
    <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
    <SortedAscendingHeaderStyle BackColor="#506C8C" />
    <SortedDescendingHeaderStyle BackColor="#6F8DAE" />
</asp:GridView>

1 个答案:

答案 0 :(得分:1)

好的,我在本地尝试了你的代码。您获得额外列的原因是因为这是您在嵌套网格视图的gridview中添加为第4列的<asp:TemplateField>,并且您使用<tr>将其包装到下一行

我们无法删除该列,如果您这样做,那么您将看不到嵌套的gridview。您可以在e.Row.Cells[3].Visible = false;事件中尝试OnRowDataBound,这将删除嵌套的网格视图。

您可以采取以下措施来解决问题。

1。您实际上并不需要两个<asp:Literal..控件,您只需执行以下操作即可。设置colspan="100%"

 <asp:TemplateField>
     <ItemTemplate>
          <tr>
            <td colspan="100%">
                <asp:GridView ID="projectCodeGridView" ....// your child gridview
            </td>
          </tr>
     </ItemTemplate>
</asp:TemplateField>

2。GrdXML (parent gridview)删除媒体资源GridLines="Both"并设置GridLines="Horizontal",您可以通过哪种方式删除列的边框。

3. 如果您需要为gridheader和gridrow单元格显示正确的边框,那么使用css指定thtd的样式并将它们链接到{{1 }和<HeaderStyle CssClass = ..。如果您需要删除特定列的边框,请使用<RowStyle CssClass = .. css属性进行调整。

以下是我应用上面建议的一些更改后得到的结果。

enter image description here