简体   繁体   中英

Export Nested Gridview into Excel

I have the following UI design for a nested GridView . According to that I want a solution for exporting a nested Gridview into Excel.

Nested GridView 单击此处显示嵌套GridView的图像

<asp:GridView ID="Mastergrid" PageSize="20" AllowPaging="true" Width="100%" Height="100%" OnPageIndexChanging="Mastergrid_PageIndexChanging" runat="server" AutoGenerateColumns="false" CssClass="gridview" DataKeyNames="r_id" OnRowDataBound="Mastergrid_RowDataBound" ShowHeaderWhenEmpty="true">
            <Columns>
                <asp:TemplateField HeaderText="id" Visible="false">
                    <ItemTemplate>
                        <asp:Label ID="lblr_id" runat="server" Text='<%# Eval("r_id") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Reg. No">
                    <ItemTemplate>
                        <asp:Label ID="lblregid" runat="server" Text='<%# Eval("r_uid") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Name">
                    <ItemTemplate>
                        <asp:Label ID="lblname" runat="server" Text='<%# Eval("name") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Indos No.">
                    <ItemTemplate>
                        <asp:Label ID="lblindosno" runat="server" Text='<%# Eval("indos_no") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>                    
                <asp:TemplateField HeaderText="Course Infomation">                             
                    <ItemTemplate>                            

                        <asp:GridView ID="subgrid" ShowHeader="true" runat="server" AutoGenerateColumns="false" Width="100%" Height="100%" EmptyDataText="No Record found..!!" CssClass="gridview1">
                            <Columns>

                                <asp:TemplateField HeaderText="Course id" Visible="false" HeaderStyle-Width="5%" >
                                    <ItemTemplate>
                                        <asp:Label ID="Course_id" runat="server" Text='<%# Eval("Course_id") %>'></asp:Label>
                                    </ItemTemplate>
                                </asp:TemplateField>

                                <asp:TemplateField HeaderText="Course Name" HeaderStyle-Width="60%">
                                    <ItemTemplate>
                                        <asp:Label ID="lblcoursename" runat="server" Text='<%# Eval("course_name") %>'></asp:Label>
                                    </ItemTemplate>
                                </asp:TemplateField>


                                <asp:TemplateField HeaderText="Course Fees" >
                                    <ItemTemplate>
                                        <asp:Label ID="lblcoursefees" runat="server" Text='<%# Eval("course_fees") %>'></asp:Label>
                                    </ItemTemplate>
                                </asp:TemplateField>


                                <asp:TemplateField HeaderText="Course Date">
                                    <ItemTemplate>
                                        <asp:Label ID="lblcoursedate" runat="server" Text='<%# Eval("course_date","{0:dd-MM-yyyy}") %>'></asp:Label>
                                    </ItemTemplate>
                                </asp:TemplateField>
                            </Columns>
                        </asp:GridView>


                    </ItemTemplate>

                </asp:TemplateField>


            </Columns>

        </asp:GridView>

You need to find the nested grid with FindControl :

    protected void Mastergrid_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "exportNestedGrid")
        {
            //convert the CommandArgument
            int rowNumber = Convert.ToInt32(e.CommandArgument);

            //find the nested GridView
            GridView gridview = GridView1.Rows[rowNumber].FindControl("subgrid") as GridView;

            //do stuff with the nested GridView
            gridview.Visible = false;
        }
    }

And for getting the rowNumber, you could send it along with the export button:

<asp:TemplateField>
    <ItemTemplate>
        <asp:Button ID="Button1" CommandArgument='<%# Container.DataItemIndex %>' CommandName="exportNestedGrid" runat="server" Text="Export nested GridView" />
    </ItemTemplate>
</asp:TemplateField>

我们可以将与简单gridview相同的Nested Gridview导出到excel中。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM