asp.net C#Gridview嵌套gridview

时间:2017-04-04 14:08:33

标签: c# asp.net gridview

亲爱的所有天才,请帮助我,这已经到处尝试,但没有运气。我试图打开我的嵌套gridview为单个父行,但打开整个父行与子行的单个记录。应该是当我单击父网格视图行的按钮时,特定行的记录应该显示在同一父行中的子网格视图中,但在我的情况下,父行的记录显示,子网格视图打开在所有父行中。请在帮助中:( enter image description here

<asp:GridView runat="server" ID="GridView4" AllowPaging="True"
    AutoGenerateColumns="False" DataKeyNames="UId" class="" CellPadding="4" PageSize="5">
    <AlternatingRowStyle BackColor="White" />
    <Columns>
        <asp:TemplateField HeaderText="File Type" Visible="false">

            <ItemTemplate>
                <asp:Label ID="lblFileType" runat="server" Text='<%# Eval("FileType") %>'></asp:Label>
            </ItemTemplate>
            <HeaderStyle HorizontalAlign="Left" VerticalAlign="Middle" Width="10%" />
            <ItemStyle HorizontalAlign="Left" VerticalAlign="Middle" Width="10%" />
        </asp:TemplateField>

        <asp:TemplateField HeaderText="Image Name" Visible="false">

            <ItemTemplate>
                <asp:Label ID="lblImageName" runat="server" Text='<%# Eval("ImageName") %>'></asp:Label>
            </ItemTemplate>
            <HeaderStyle HorizontalAlign="Left" VerticalAlign="Middle" Width="10%" />
            <ItemStyle HorizontalAlign="Left" VerticalAlign="Middle" Width="10%" />
        </asp:TemplateField>

        <asp:TemplateField HeaderText="">
            <ItemTemplate>
                <asp:ImageButton ID="Image2" class="img1" runat="server" ImageUrl='<%# Bind("pic") %>' OnCommand="blah_Command" CommandName='<%# Eval("UId") %>' CommandArgument='<%# Eval("pic") %>' />

                <asp:Panel ID="pnlOrders" runat="server" Style="position: relative" Visible="false">
                    <asp:GridView ID="gvOrders" runat="server" AutoGenerateColumns="false" CssClass="ChildGrid">
                        <Columns>
                            <asp:BoundField ItemStyle-Width="150px" DataField="FileSize" HeaderText="Order Id" />
                            <asp:BoundField ItemStyle-Width="150px" DataField="ShareByUserId" HeaderText="Date" />
                        </Columns>
                    </asp:GridView>
                </asp:Panel>

            </ItemTemplate>

        </asp:TemplateField>

    </Columns>
    <FooterStyle BackColor="#ffffff" Font-Bold="True" ForeColor="White" />
    <HeaderStyle BackColor="#ffffff" Font-Bold="True" ForeColor="White" />

</asp:GridView>

代码背后:

protected void blah_Command(object sender, CommandEventArgs e)
{
    foreach (GridViewRow row2 in GridView4.Rows)
    {
        row2.FindControl("pnlOrders").Visible = true;

        string customerId = e.CommandName.ToString();
        GridView gvOrders = row2.FindControl("gvOrders") as GridView;
        BindOrders(customerId, gvOrders);
    }
}

private void BindOrders(string customerId, GridView gvOrders)
{
    gvOrders.ToolTip = customerId;
    gvOrders.DataSource = GetData(string.Format("select * from SShare where UId='{0}'", customerId));
    gvOrders.DataBind();
} 

1 个答案:

答案 0 :(得分:0)

您正在循环GridView4中的所有行,因此每个嵌套的GridView都将填充数据。 你需要的是被点击的LinkBut​​ton所在的行的行号,并使用它来查找嵌套的GridView。

protected void blah_Command(object sender, CommandEventArgs e)
{
    //get the current datagrid item from the sender
    GridViewRow row = (GridViewRow)(((Control)sender).NamingContainer);

    //the row index of the gridviewrow
    int rowIndex = row.RowIndex;

    //find the correct nested grid using the row index
    GridView gvOrders = GridView4.Rows[rowIndex].FindControl("gvOrders") as GridView;
}