This is a migrated thread and some comments may be shown as answers.

RadGrid with child RadGrid in NestedViewTemplate Export to Excel Problem

2 Answers 111 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Shawn
Top achievements
Rank 1
Shawn asked on 23 Sep 2015, 12:00 PM

Hello,

I'm trying to export the contents of a RadGrid that has a child RadGrid in its NestedViewTemplate to Excel format.  I've found some examples on how to export the contents of a RadGrid to Excel, however, those examples only seem to export the parent radgrid's contents and not the child content.  Is there a way to do this?  An example would be very much appreciated.  Thanks.

Here's my code for the RadGrid:

<telerik:RadButton ID="btnExportToExcel" runat="server" Text="Export to Excel" OnClick="btnExportToExcel_Click"></telerik:RadButton>
 
<telerik:RadGrid ID="grdViewInventory" runat="server"
    AutoGenerateColumns="False"
    AllowPaging="false"
    AllowSorting="false"
    OnColumnCreated="grdViewInventory_ColumnCreated"
    OnNeedDataSource="grdViewInventory_NeedDataSource"
    Skin="Metro"
    ShowHeader="false"
    ShowFooter="false"
    CssClass="IV_MasterGrid" CellPadding="0" CellSpacing="0">
    <MasterTableView HierarchyDefaultExpanded="true" HierarchyLoadMode="ServerOnDemand" EnableNoRecordsTemplate="true" ShowHeadersWhenNoRecords="true" DataKeyNames="DataKey" RetainExpandStateOnRebind="true">
        <NoRecordsTemplate>
            <div class="div_NoRecordsTemplate">No Components Found.</div>
        </NoRecordsTemplate>
        <NestedViewSettings>
            <ParentTableRelation>
                <telerik:GridRelationFields DetailKeyField="DataKey" MasterKeyField="DataKey" />
            </ParentTableRelation>
        </NestedViewSettings>
        <NestedViewTemplate>
            <telerik:RadGrid ID="rgNestedViewInventory" runat="server" AutoGenerateColumns="false" Width="99.5%" CssClass="IV_NestedGrid" OnNeedDataSource="rgNestedViewInventory_NeedDataSource">
                <MasterTableView EnableNoRecordsTemplate="true" ShowHeadersWhenNoRecords="true" TableLayout="Fixed">
                    <Columns>
                        <telerik:GridBoundColumn DataField="FilterType" HeaderText="Filter Type" ReadOnly="True" UniqueName="FilterType">
                            <HeaderStyle CssClass="IV_FT_Nested" Wrap="false" />
                            <ItemStyle CssClass="IV_FT_Nested" Wrap="false" />
                        </telerik:GridBoundColumn>
                        <telerik:GridBoundColumn DataField="ModelNumber" HeaderText="Model Number" ReadOnly="True" UniqueName="ModelNumber">
                            <HeaderStyle CssClass="IV_MN_Nested" Wrap="false" />
                            <ItemStyle CssClass="IV_MN_Nested" Wrap="false" />
                        </telerik:GridBoundColumn>
                        <telerik:GridBoundColumn DataField="PartNumber" HeaderText="Part Number" ReadOnly="True" UniqueName="PartNumber">
                            <HeaderStyle CssClass="IV_PN_Nested" Wrap="false" />
                            <ItemStyle CssClass="IV_PN_Nested" Wrap="false" />
                        </telerik:GridBoundColumn>
                        <telerik:GridBoundColumn DataField="FilterDimension" HeaderText="Filter Size" ReadOnly="True" UniqueName="FilterDimension">
                            <HeaderStyle CssClass="IV_FDE_Nested" Wrap="false" />
                            <ItemStyle CssClass="IV_FDE_Nested" Wrap="false" />
                        </telerik:GridBoundColumn>
                        <telerik:GridBoundColumn DataField="Quantity" HeaderText="Quantity" ReadOnly="True" UniqueName="FilterManufacturer">
                            <HeaderStyle CssClass="IV_QTY_Nested" Wrap="false" />
                            <ItemStyle CssClass="IV_QTY_Nested" Wrap="false" />
                        </telerik:GridBoundColumn>
                    </Columns>
                </MasterTableView>
            </telerik:RadGrid>
        </NestedViewTemplate>
        <Columns>
            <telerik:GridTemplateColumn UniqueName="GroupHeaderColumn" Resizable="false" SortExpression="PropertyNumber">
                <ItemTemplate>
                    <asp:Label runat="server" ID="Label1" Text='<%# Eval("Building") %>' />
                    <asp:Label runat="server" ID="Label2" Text='<%# " - (" + Eval("PropertyNumber") + ") - " %>' />
                    <asp:Label runat="server" ID="Label4" Text='<%# Eval("UnitTag") %>' />
                    <asp:Label runat="server" ID="Label3" Text='<%# " - " + Eval("AHUType") %>' />
                    <asp:Label runat="server" ID="Label5" Text='<%# " - " + Eval("Configuration") %>' />
                </ItemTemplate>
                <ItemStyle CssClass="IV_MasterGrid_GroupHeader" />
            </telerik:GridTemplateColumn>
        </Columns>
    </MasterTableView>
</telerik:RadGrid>

 

Here's my C# code-behind:

protected void grdViewInventory_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
{
    siteID = ApplicationDTO.GetSiteID();
    grdViewInventory.DataSource = BLL.GetInventoryHeader(siteID).GroupBy(x => x.DataKey).Select(y => y.First()).OrderBy(z => z.Building);
}
 
protected void grdViewInventory_ColumnCreated(object sender, GridColumnCreatedEventArgs e)
{
    //Hides the expand/collapse column
    if (e.Column is GridExpandColumn)
    {
        e.Column.Visible = false;
    }
}
 
protected void btnExportToExcel_Click(object sender, EventArgs e)
{
    grdViewInventory.ExportSettings.OpenInNewWindow = true;
    grdViewInventory.ExportSettings.ExportOnlyData = true;
    grdViewInventory.ExportSettings.IgnorePaging = true;
    grdViewInventory.ExportSettings.Excel.Format = GridExcelExportFormat.Xlsx;
    grdViewInventory.MasterTableView.Items[0].Expanded = true;
    RadGrid rgNestedViewInventory = (RadGrid)grdViewInventory.MasterTableView.Items[0].ChildItem.FindControl("rgNestedViewInventory");
    rgNestedViewInventory.Rebind();
    grdViewInventory.MasterTableView.HierarchyDefaultExpanded = true;
    grdViewInventory.MasterTableView.ExportToExcel();
}
 
protected void rgNestedViewInventory_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
{
    RadGrid rgChild = (RadGrid)sender;
    GridNestedViewItem nesteditem = (GridNestedViewItem)rgChild.NamingContainer;
    string dataKey = Convert.ToString(nesteditem.ParentItem.GetDataKeyValue("DataKey"));
    (sender as RadGrid).DataSource = BLL.GetInventory(siteID).Where(x => x.DataKey == dataKey).OrderBy(y => y.PropertyNumber);
}

2 Answers, 1 is accepted

Sort by
0
Kostadin
Telerik team
answered on 28 Sep 2015, 08:20 AM
Hi Shawn,

I noticed that you are using XLSX export format. You should note that this export format does not support hierarchy and I would recommend you to use HTML-based one. Additionally you do not need to Rebind the inner tables and you can also expand all of them by enabling HierarchyDefaultExpanded to the parent table. Also I would recommend you to examine the following code library which demonstrates how to achieve your requirement.

Regards,
Kostadin
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
0
Shawn
Top achievements
Rank 1
answered on 28 Sep 2015, 11:12 AM

Hello Kostadin,

Thanks you!  that was exactly what I needed.  I had not thought of changing the format to HTML because I thought it was going to give me an HTML page, but I was wrong.  Thanks for your help!

Regards,

Shawn A.

Tags
Grid
Asked by
Shawn
Top achievements
Rank 1
Answers by
Kostadin
Telerik team
Shawn
Top achievements
Rank 1
Share this question
or