RadGrid : Export to Word not Working in multi - level hierachy

16 posts, 0 answers
  1. Rahul Kasar
    Rahul Kasar avatar
    14 posts
    Member since:
    Nov 2009

    Posted 11 Dec 2013 Link to this post

    Hi ,

    I am using Telerik RadGrid in my application having multilevel hierachy i.e the Master table has a detail table which again has one more detail table.Export to excel with same grid is working fine but I am facing the following two issues with export to word.
    1.  When I try to export the grid details to word only the first row of the grid is expanded and completely exported and the other rows are not not expanded only the Master Table rows are displayed.
    2. When I again click the export to word only the parent i.e master table rows are exported to the doc file.

    I have used the disable ajax for export and set the following properties to the grid

     
    grid.MasterTableView.HierarchyDefaultExpanded = true;
                if (grid.MasterTableView.HasDetailTables)
                {
                    foreach (GridTableView gridTableView in grid.MasterTableView.DetailTables)
                    {
                        // Added by partner CB to Fix defect 15597
                        gridTableView.AllowPaging = false;
                        gridTableView.HierarchyDefaultExpanded = true;
                    }
                }
               
    grid.ItemCreated += new GridItemEventHandler(RadGrid_ItemCreated);
                grid.ExportSettings.ExportOnlyData = true;
                grid.ExportSettings.IgnorePaging = true;
                grid.ExportSettings.OpenInNewWindow = true;

      grid.MasterTableView.ExpandCollapseColumn.Display = false;
            grid.MasterTableView.HierarchyLoadMode = GridChildLoadMode.Client;
            grid.MasterTableView.DetailTables[0].ExpandCollapseColumn.Display = false;
            grid.MasterTableView.DetailTables[0].DetailTables[0].ExpandCollapseColumn.Display = false;


    Please help me in this.

    Thanks,
  2. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 12 Dec 2013 Link to this post

    Hi Rahul Kasar,

    I tried to export a 3 Level hierarchy Grid and it works fine. Here is a sample code snippet.

    ASPX:
    <asp:Button ID="Button1" runat="server" Text="Export To Word" onclick="Button1_Click" />

    C#:
    protected void Button1_Click(object sender, EventArgs e)
    {
        RadGrid1.ExportSettings.IgnorePaging = true;
        RadGrid1.ExportSettings.ExportOnlyData = true;
        RadGrid1.MasterTableView.HierarchyDefaultExpanded = true;
        if (RadGrid1.MasterTableView.HasDetailTables)
        {
            foreach (GridTableView gridTableView in RadGrid1.MasterTableView.DetailTables)
            {            
                gridTableView.HierarchyDefaultExpanded = true;
            }
        }
        RadGrid1.MasterTableView.ExportToWord();
    }

    Thanks,
    Princy

  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Rahul Kasar
    Rahul Kasar avatar
    14 posts
    Member since:
    Nov 2009

    Posted 24 Dec 2013 Link to this post

    Hi,

    Thanks for your reply. I am using the same code mentioned below. But this is working for Export to excel but not

    Export to Word. Also I have the MasterTableView


    Below is the Grid Structure :
       <telerik:RadGrid ID="RadGrid1" OnNeedDataSource="RadGrid1_NeedDataSource"
                                        runat="server" Width="99.4%" OnDetailTableDataBind="RadGrid1_DetailTableDataBind"
                                        OnItemDataBound="RadGrid1_ItemDataBound" OnItemCreated="RadGrid1_ItemCreated"
                                        OnPageIndexChanged="RadGrid1_PageIndexChanged" >
                                        <ClientSettings>
                                            <Resizing AllowColumnResize="true" EnableRealTimeResize="true" ResizeGridOnColumnResize="true"
                                                ClipCellContentOnResize="true" />
                                        </ClientSettings>
                                        <MasterTableView DataKeyNames="ID,Name" HierarchyLoadMode="ServerOnDemand"
                                            HierarchyDefaultExpanded="false" TableLayout="Fixed"  CommandItemDisplay="Top">
                                            <CommandItemTemplate>
                                                <table width="100%" >
                                                    <tr>
                                                        <td>
                                                        <td >
                                                            <asp:ImageButton ID="ExcelImageButton"
                                                                runat="server" OnClick="ExcelImageButton_Click" />
                                                        </td>
                                                        <td >
                                                            <asp:ImageButton ID="WordImageButton"
                                                                runat="server" OnClick="WordImageButton_Click" />
                                                        </td>
                                                        <td >
                                                            <asp:ImageButton ID="CSVImageButton"
                                                                runat="server" OnClick="CsvImageButton_Click" />
                                                        </td>
                                                        </td>
                                                    </tr>
                                                </table>
                                            </CommandItemTemplate>
                                            <Columns>
                                                <telerik:GridBoundColumn SortExpression="Name" HeaderText="" HeaderButtonType="TextButton"
                                                    DataField="Name">
                                            
                                                </telerik:GridBoundColumn>
                                            </Columns>
                                            <DetailTables>
                                                <telerik:GridTableView DataKeyNames="DetailID,ID" GridLines="None" HierarchyLoadMode="Client" HierarchyDefaultExpanded="true">
                                                    <PagerStyle Visible="false" />
                                                    <ParentTableRelation>
                                                        <telerik:GridRelationFields DetailKeyField="ID" MasterKeyField="ID" />
                                                    </ParentTableRelation>
                                                    <Columns>
                                                        <telerik:GridBoundColumn DataField="DetailDesc" HeaderText="Configuration Name"
                                                            UniqueName="DetailDesc">
                                                        </telerik:GridBoundColumn>
                                                        <telerik:GridBoundColumn DataField="ITem" HeaderText="" UniqueName="ITem">
                                                          
                                                        </telerik:GridBoundColumn>
                                                      
                                                    </Columns>
                                                    <DetailTables>
                                                        <telerik:GridTableView DataKeyNames="SencondLevelID,ID" Width="100%" GridLines="None"
                                                            HierarchyLoadMode="Client" AllowCustomPaging="true">
                                                            <ParentTableRelation>
                                                                <telerik:GridRelationFields DetailKeyField="SencondLevelID" MasterKeyField="ID" />
                                                            </ParentTableRelation>
                                                            <Columns>
                                                                <telerik:GridTemplateColumn HeaderText="Action" UniqueName="TemplateColumn" Resizable="false" Reorderable="false">
                                                                    <ItemTemplate>
                                                                        <asp:HyperLink ID="ModifyLink" runat="server" Text="Modify"></asp:HyperLink>
                                                                    </ItemTemplate>
                                                                </telerik:GridTemplateColumn>
                                                                <telerik:GridBoundColumn DataField="SencondLevelNumber" HeaderText="SencondLevel Number"
                                                                    UniqueName="SencondLevelNumber" SortExpression="SencondLevelNumber">
                                                                    <HeaderStyle Width="10%" />
                                                                </telerik:GridBoundColumn>
                                                                <telerik:GridBoundColumn DataField="Number" HeaderText="Number" UniqueName="Number"
                                                                    SortExpression="Number">
                                                                </telerik:GridBoundColumn>
                                                                <telerik:GridBoundColumn DataField="Company" HeaderText="Company" UniqueName="Company"
                                                                    SortExpression="Company">
                                                                    <HeaderStyle Width="10%" />
                                                                </telerik:GridBoundColumn>
                                                                <telerik:GridBoundColumn DataField="Date/Time" HeaderText="Date/Time" UniqueName="Date/Time"
                                                                    SortExpression="Date/Time">
                                                                    <HeaderStyle Width="10%" />
                                                                </telerik:GridBoundColumn>
                                                                <telerik:GridBoundColumn DataField="Location" HeaderText="ITem Location" UniqueName="Location" SortExpression="Location">
                                                                 
                                                                </telerik:GridBoundColumn>
                                                              
                                                                <telerik:GridBoundColumn DataField="ITemType" HeaderText="ITem Type" UniqueName="ITemType"
                                                                    SortExpression="ITemType">
                                                                </telerik:GridBoundColumn>
                                                              
                                                            </Columns>
                                                        </telerik:GridTableView>
                                                    </DetailTables>
                                                </telerik:GridTableView>
                                            </DetailTables>
                                        </MasterTableView>
                                    </telerik:RadGrid>

    and the following is the server code :

    grid.MasterTableView.HierarchyDefaultExpanded = true;
                if (grid.MasterTableView.HasDetailTables)
                {
                    foreach (GridTableView gridTableView in grid.MasterTableView.DetailTables)
                    {
                 
                        gridTableView.AllowPaging = false;
                        gridTableView.HierarchyDefaultExpanded = true;
                    }
                }
               
    grid.ItemCreated += new GridItemEventHandler(RadGrid_ItemCreated);
                grid.ExportSettings.ExportOnlyData = true;
                grid.ExportSettings.IgnorePaging = true;
                grid.ExportSettings.OpenInNewWindow = true;

      grid.MasterTableView.ExpandCollapseColumn.Display = false;
            grid.MasterTableView.HierarchyLoadMode = GridChildLoadMode.Client;
            grid.MasterTableView.DetailTables[0].ExpandCollapseColumn.Display = false;
            grid.MasterTableView.DetailTables[0].DetailTables[0].ExpandCollapseColumn.Display = false;

    Please help me in this.

    Thanks



  5. Eyup
    Admin
    Eyup avatar
    3015 posts

    Posted 25 Dec 2013 Link to this post

    Hello Rahul,

    I have created a sample RadGrid web site to test the described behavior. Can you please run the attached application and verify that it works as expected on your side, too?

    Regards,
    Eyup
    Telerik
    If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to the blog feed now.
  6. Rahul Kasar
    Rahul Kasar avatar
    14 posts
    Member since:
    Nov 2009

    Posted 26 Dec 2013 Link to this post

    Hi,

    Thanks for the reply the sample you have sent has only one level of hierarchy in my grid I have added a detail table in the details table of the Master Table. I have used the similar settings for export to excel and its working but its not working in case of export to word. I am still facing the following two issues :
    1.  When I try to export the grid details to word only the first row of the grid is expanded and completely exported and the other rows are not not expanded only the Master Table rows are displayed.
    2. When I again click the export to word only the parent i.e master table rows are exported to the doc file.

    Please help me in this.

    Thanks,
  7. Eyup
    Admin
    Eyup avatar
    3015 posts

    Posted 02 Jan 2014 Link to this post

    Hi Rahul,

    I have added multiple levels to the hierarchy and the result is same. Please run the attached web site and instruct me the exact steps I need to follow to reproduce the issue.

    Regards,
    Eyup
    Telerik
    If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to the blog feed now.
  8. Rahul Kasar
    Rahul Kasar avatar
    14 posts
    Member since:
    Nov 2009

    Posted 12 Feb 2014 in reply to Eyup Link to this post

    Hi,

    Thanks for the response, I am still getting this issue at my machine with RadGrid having only one detail table in the master table. When I try to export to excel it works fine , but when I try to export to word the detail table is not exported. Is this something with ajax ?. Please suggest

    Please help me in this.

    Thanks,
  9. Rahul Kasar
    Rahul Kasar avatar
    14 posts
    Member since:
    Nov 2009

    Posted 13 Feb 2014 in reply to Rahul Kasar Link to this post

    Hi,

    Please help me in this.

    Thanks,
  10. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 13 Feb 2014 in reply to Rahul Kasar Link to this post

    Hi Rahul Kasar,

    Can you please try Rebinding the Grid on Export to Word, in the OnClick event WordImageButton_Click():

    C#:
    RadGrid1.MasterTableView.HierarchyDefaultExpanded = true;
    RadGrid1.MasterTableView.DetailTables[0].HierarchyDefaultExpanded = true;
    RadGrid1.Rebind();
    RadGrid1.MasterTableView.ExportToWord();

    Thanks,
    Princy
  11. Rahul Kasar
    Rahul Kasar avatar
    14 posts
    Member since:
    Nov 2009

    Posted 13 Feb 2014 in reply to Princy Link to this post

    Hi Princy,

    Thanks for the quick response. I tried the same but still getting the same issue :

     RadGrid1.MasterTableView.ExpandCollapseColumn.Display = false;
            RadGrid1.MasterTableView.DetailTables[0].ExpandCollapseColumn.Display = false;
            RadGrid1.MasterTableView.DetailTables[0].CommandItemDisplay = GridCommandItemDisplay.None;
            GridTemplateColumn gridTemplateColumn = RadGrid1.MasterTableView.DetailTables[0].Columns.FindByUniqueName("GridTemplateColumn") as GridTemplateColumn;
            if (templateEditColumn != null)
                templateEditColumn.Visible = false;
            RadGrid1.MasterTableView.Columns.FindByUniqueName(columnUniqueName).Display = false;
            RadGrid1.MasterTableView.HierarchyDefaultExpanded = true;
            RadGrid1.ItemCreated += new GridItemEventHandler(RadGrid_ItemCreated);
            RadGrid1.ExportSettings.ExportOnlyData = true;
            RadGrid1.ExportSettings.IgnorePaging = true;
            RadGrid1.ExportSettings.OpenInNewWindow = true;
            RadGrid1.ExportSettings.FileName = fileName;
            if (RadGrid1.MasterTableView.HasDetailTables)
            {
                foreach (GridTableView gridTableView in RadGrid1.MasterTableView.DetailTables)
                {
                    gridTableView.AllowPaging = false;
                    gridTableView.HierarchyDefaultExpanded = true;
               //     RadGrid1.MasterTableView.DetailTables[0].Rebind();
        RadGrid1.Rebind();
                }
            }

            RadGrid1.MasterTableView.ExportToWord();

    I tried with both options to rebind the detail table and the parent grid but didnt worked.

    Please suggest.

    Thanks,
  12. Rahul Kasar
    Rahul Kasar avatar
    14 posts
    Member since:
    Nov 2009

    Posted 13 Feb 2014 Link to this post

    Hi ,

    Please help in this. This is really very urgent.

    Thanks,
  13. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 13 Feb 2014 in reply to Rahul Kasar Link to this post

    Hi Rahul Kasar,

    I'm not able to replicate the issue at my end. Here is a sample code snippet that i tried which works fine as expected:

    ASPX:
    <telerik:RadGrid ID="RadGrid1" OnNeedDataSource="RadGrid1_NeedDataSource" runat="server" AutoGenerateColumns="false" OnDetailTableDataBind="RadGrid1_DetailTableDataBind">
        <MasterTableView DataKeyNames="CustomerID" CommandItemDisplay="Top">
            <CommandItemTemplate>
                <asp:ImageButton ID="WordImageButton" runat="server" AlternateText="Export To Word"
                    OnClick="WordImageButton_Click" />
            </CommandItemTemplate>
            <Columns>
                <telerik:GridBoundColumn HeaderText="CustomerID" DataField="CustomerID">
                </telerik:GridBoundColumn>
                <telerik:GridBoundColumn DataField="ContactName" HeaderText="ContactName" UniqueName="ContactName">
                </telerik:GridBoundColumn>
            </Columns>
            <DetailTables>
                <telerik:GridTableView DataKeyNames="OrderID" Name="Orders">
                    <ParentTableRelation>
                        <telerik:GridRelationFields DetailKeyField="CustomerID" MasterKeyField="CustomerID" />
                    </ParentTableRelation>
                    <Columns>
                        <telerik:GridBoundColumn DataField="OrderID" HeaderText="OrderID" UniqueName="OrderID">
                        </telerik:GridBoundColumn>
                        <telerik:GridBoundColumn DataField="ShipName" HeaderText="ShipName" UniqueName="ShipName">
                        </telerik:GridBoundColumn>
                    </Columns>
                    <DetailTables>
                        <telerik:GridTableView DataKeyNames="OrderID" Name="OrderDetails">
                            <ParentTableRelation>
                                <telerik:GridRelationFields DetailKeyField="OrderID" MasterKeyField="OrderID" />
                            </ParentTableRelation>
                            <Columns>
                                <telerik:GridBoundColumn DataField="UnitPrice" HeaderText="UnitPrice" UniqueName="UnitPrice">
                                </telerik:GridBoundColumn>
                            </Columns>
                        </telerik:GridTableView>
                    </DetailTables>
                </telerik:GridTableView>
            </DetailTables>
        </MasterTableView>
    </telerik:RadGrid>

    C#:
    protected void RadGrid1_NeedDataSource(object source, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
    {
        if (!e.IsFromDetailTable)
        {
            RadGrid1.DataSource = GetDataTable("SELECT * FROM Customers");
        }
    }
     
    protected void RadGrid1_DetailTableDataBind(object source, Telerik.Web.UI.GridDetailTableDataBindEventArgs e)
    {
        GridDataItem dataItem = (GridDataItem)e.DetailTableView.ParentItem;
        switch (e.DetailTableView.Name)
        {
            case "Orders":
                {
                    string CustomerID = dataItem.GetDataKeyValue("CustomerID").ToString();
                    e.DetailTableView.DataSource = GetDataTable("SELECT * FROM Orders WHERE CustomerID = '" + CustomerID + "'");
                    break;
                }
            case "OrderDetails":
                {
                    string OrderID = dataItem.GetDataKeyValue("OrderID").ToString();
                    e.DetailTableView.DataSource = GetDataTable("SELECT * FROM [Order Details] WHERE OrderID = " + OrderID);
                    break;
                }
        }
    }
     
    public DataTable GetDataTable(string query)
    {
        String ConnString = ConfigurationManager.ConnectionStrings["Northwind_newConnectionString3"].ConnectionString;
        SqlConnection conn = new SqlConnection(ConnString);
        SqlDataAdapter adapter = new SqlDataAdapter();
        adapter.SelectCommand = new SqlCommand(query, conn);
        DataTable myDataTable = new DataTable();
     
        conn.Open();
        try
        {
            adapter.Fill(myDataTable);
        }
        finally
        {
            conn.Close();
        }
        return myDataTable;
    }
     
    protected void WordImageButton_Click(object sender, ImageClickEventArgs e)
        RadGrid1.MasterTableView.HierarchyDefaultExpanded = true;
        RadGrid1.MasterTableView.DetailTables[0].HierarchyDefaultExpanded = true;
        RadGrid1.ExportSettings.ExportOnlyData = true;
        RadGrid1.ExportSettings.IgnorePaging = true;
        RadGrid1.ExportSettings.OpenInNewWindow = true;  
        RadGrid1.MasterTableView.ExportToWord();
    }

    Thanks,
    Princy
  14. Rahul Kasar
    Rahul Kasar avatar
    14 posts
    Member since:
    Nov 2009

    Posted 14 Feb 2014 in reply to Princy Link to this post

    Hi Princy,

    Thanks for the response. In my case the grid with detail table is exported properly to excel. But incase of word it fails to export detail table. I have used similar settings for word and excel export.

    Please suggest.

    Thanks,
  15. Rahul Kasar
    Rahul Kasar avatar
    14 posts
    Member since:
    Nov 2009

    Posted 14 Feb 2014 in reply to Rahul Kasar Link to this post

    Hi Princy,

    I have resolved this issue. In the Item Created event of the Grid there was a code to expand /collapse the grid items. I have manipulated the code to resolve the issue.

    Thanks for your support

    Thanks,
  16. Karu
    Karu avatar
    3 posts
    Member since:
    Feb 2012

    Posted 17 Jul 2014 Link to this post

    Using Export to PDF - I tried to export a 3 Level hierarchy Grid and it works fine. when It is more than 3 I am not able to get those items. Help me . 
  17. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 18 Jul 2014 in reply to Karu Link to this post

    Hi Karu,

    I was not able to replicate this issue at my end. Make sure you have expanded all level on export.

    C#:
    RadGrid1.MasterTableView.HierarchyDefaultExpanded = true; //Expand 1st level
    RadGrid1.MasterTableView.DetailTables[0].HierarchyDefaultExpanded = true;//Expand 2nd level   
    RadGrid1.MasterTableView.DetailTables[0].DetailTables[0].HierarchyDefaultExpanded = true;//Expand 3rd level
    //so on..

    Thanks,
    Princy
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017