Export DetailTable to Excel

14 posts, 0 answers
  1. Adam Hey
    Adam Hey avatar
    8 posts
    Member since:
    Apr 2010

    Posted 21 Jul 2010 Link to this post

    Hi all,
       I have a RadGrid with 2 levels of data hierarchy (Countries > Campaigns > Entries) . I only want to export the lowest level grid (Entries) to Excel.

    For some reason, the Export isn't happening. The ItemCommand event with the CampaignsRadGrid.MasterTableView.ExportToExcel(); call is being hit though, but the only thing that happens is that the pagination controls, on the bottom of the grid, disappear.

    ASPX code:
        <telerik:RadAjaxManagerProxy ID="RadAjaxManager1" runat="server">
            <AjaxSettings>
                <telerik:AjaxSetting AjaxControlID="CampaignsRadGrid">
                    <UpdatedControls>
                       <telerik:AjaxUpdatedControl ControlID="CampaignsRadGrid" LoadingPanelID="RadAjaxLoadingPanel1" />
                    </UpdatedControls>
                </telerik:AjaxSetting>
            </AjaxSettings>
        </telerik:RadAjaxManagerProxy>
        <telerik:RadAjaxLoadingPanel ID="RadAjaxLoadingPanel1" runat="server" Skin="Vista"></telerik:RadAjaxLoadingPanel>
     
     
    <telerik:RadGrid ID="CampaignsRadGrid" Width="97%" Skin="Office2007"
    AllowPaging="True"  PageSize="10" runat="server" AllowSorting="true"
    AutoGenerateColumns="false" GridLines="None"
        onneeddatasource="CampaignsRadGrid_NeedDataSource"
        ondetailtabledatabind="CampaignsRadGrid_DetailTableDataBind"
        onitemcommand="CampaignsRadGrid_ItemCommand"
        >
        <PagerStyle Mode="NextPrevNumericAndAdvanced" AlwaysVisible="true" />
        <MasterTableView Width="100%" EditMode="PopUp" DataKeyNames="ID" AllowMultiColumnSorting="True" >
            <DetailTables>
                <telerik:GridTableView DataKeyNames="ID" Name="Campaigns" Width="100%" NoDetailRecordsText="There are no campaigns for this country.">
                    <DetailTables>
                        <telerik:GridTableView CommandItemDisplay="Top" DataKeyNames="ID" AutoGenerateColumns="true" Name="Entries" NoDetailRecordsText="There are no entries for this campaign." Width="100%">
                        <CommandItemSettings  ShowExportToExcelButton="true" ShowRefreshButton="false" ShowAddNewRecordButton="false" />
                        </telerik:GridTableView>
                    </DetailTables>
                    <Columns>
                        <telerik:GridBoundColumn UniqueName="CampaignName" DataField="CampaignName" SortExpression="CampaignName" HeaderText="Campaign Name" AllowSorting="true" />
                        <telerik:GridDateTimeColumn  UniqueName="DateCreated" SortExpression="DateCreated" DataField="DateCreated" HeaderText="Date Created" DataFormatString="{0:d}" AllowSorting="true" />
                        <telerik:GridBoundColumn UniqueName="Entries" SortExpression="Entries" DataField="Entries" HeaderText="Entries" AllowSorting="true" />
                    </Columns>
                </telerik:GridTableView>
            </DetailTables>
            <Columns>
                <telerik:GridBoundColumn UniqueName="CountryName" SortExpression="CountryName" DataField="CountryName" HeaderText="CountryName" AllowSorting="true" />
            </Columns>
     
        </MasterTableView>
        <ClientSettings EnableRowHoverStyle="true" />
        <PagerStyle Mode="NextPrevAndNumeric" />
     
    </telerik:RadGrid>


    C# code:
    protected void CampaignsRadGrid_ItemCommand(object source, GridCommandEventArgs e)
    {
        if (e.CommandName == Telerik.Web.UI.RadGrid.ExportToExcelCommandName)
        {
            ConfigureExport();
            CampaignsRadGrid.MasterTableView.ExportToExcel();
        }
    }
     
    public void ConfigureExport()
    {
        CampaignsRadGrid.ExportSettings.ExportOnlyData = true;
        CampaignsRadGrid.ExportSettings.IgnorePaging = true;
        CampaignsRadGrid.ExportSettings.OpenInNewWindow = false;
    }

    Any thoughts? Thanks in advance for any help...
  2. Chandran Chandran
    Chandran Chandran avatar
    53 posts
    Member since:
    Apr 2010

    Posted 21 Jul 2010 Link to this post

    Hi,
     Try to use the below code to make it functioning. Hope it may help

    <script type="text/javascript" language="javascript">        
           function onRequestStart(sender, args) {
               if (args.get_eventTarget().indexOf("ExportToExcelButton") >= 0 ||
                       args.get_eventTarget().indexOf("ExportToPdfButton") >= 0) {
                   args.set_enableAjax(false);
               }
           }    
       </script>

    <telerik:RadAjaxManager ID="radAjaxManager" runat="server">
                <ClientEvents OnRequestStart="onRequestStart" />
                <AjaxSettings>
                    <telerik:AjaxSetting AjaxControlID="RadSummary">
                        <UpdatedControls>
                            <telerik:AjaxUpdatedControl ControlID="RadSummary" LoadingPanelID="RadAjaxLoadingPanel1" />
                        </UpdatedControls>
                    </telerik:AjaxSetting>
                </AjaxSettings>
              </telerik:RadAjaxManager>

    -Thanx
  3. Adam Hey
    Adam Hey avatar
    8 posts
    Member since:
    Apr 2010

    Posted 21 Jul 2010 Link to this post

    Thanks Chandran,
       Unfortunately I get the same result, but with javascript error:
    Sys.InvalidOperationException: A control is already 
    associated with the element.

    Firebug pointed the error to this function
    Sys.UI.Control = function Sys$UI$Control(element) {
        /// <summary locid="M:J#Sys.UI.Control.#ctor" />
        /// <param name="element" domElement="true"></param>
        var e = Function._validateParams(arguments, [
            {name: "element", domElement: true}
        ]);
        if (e) throw e;
        if (typeof(element.control) != 'undefined') throw Error.invalidOperation(Sys.Res.controlAlreadyDefined);
        Sys.UI.Control.initializeBase(this);
        this._element = element;
        element.control = this;
    }
  4. Adam Hey
    Adam Hey avatar
    8 posts
    Member since:
    Apr 2010

    Posted 22 Jul 2010 Link to this post

    AH! I noticed I had put the RadGrid inside an update panel.
    Removed this and the export works. only problem is that it seems to be exporting the master table's data, but with the detail table's fields. because the only common field between these two is "ID" that is the only column that contains data (in the exported Excel file).

    Is it necessary to do a Rebind() or something similar? or should I be targeting the DetailTable when calling the ExportToExcel() method?

    i.e.
    CampaignsRadGrid.MasterTableView.DetailTables[0].DetailTables[0].ExportToExcel();

    instead of
    CampaignsRadGrid.MasterTableView.ExportToExcel();

    I'll try a few things and if I find a solution myself, I'll put it up here...
  5. Adam Hey
    Adam Hey avatar
    8 posts
    Member since:
    Apr 2010

    Posted 22 Jul 2010 Link to this post

    I found out that:
    CampaignsRadGrid.ExportSettings.IgnorePaging = true;
     will cause the RadGrid to Rebind, which makes sense 'cause when the export method was called, the DetailTable had the same DataSource as the MasterTable.

    setting IgnorePaging = false fixed my problem.
    see here: http://www.telerik.com/community/forums/aspnet-ajax/grid/export-multi-level-hierarchy-radgrid.aspx#920000
  6. Adam Hey
    Adam Hey avatar
    8 posts
    Member since:
    Apr 2010

    Posted 23 Jul 2010 Link to this post

    aaargh. more problems with this grid.
    In the Hierarchical Table I set Display="false" on certain columns to show a 'summary' of the data. No matter what I try, I cannot get those columns to export to Excel - it only ever exports the visible ones.
    Grid:
    <Columns>
        <telerik:GridBoundColumn Display="false" UniqueName="ID" DataField="ID" SortExpression="ID" HeaderText="ID" AllowSorting="true" />
        <telerik:GridBoundColumn Display="false" UniqueName="CampaignName" DataField="CampaignName" SortExpression="CampaignName" HeaderText="CampaignName" AllowSorting="true" />
        <telerik:GridDateTimeColumn  UniqueName="DateCreated" SortExpression="DateCreated" DataField="DateCreated" HeaderText="Date Created" DataFormatString="{0:d}" AllowSorting="true" />

        <telerik:GridDateTimeColumn Display="false" UniqueName="DOB" SortExpression="DOB" DataField="DOB" HeaderText="DOB" DataFormatString="{0:d}" AllowSorting="true" />
        <telerik:GridBoundColumn Display="false" UniqueName="VariableField1" SortExpression="VariableField1" DataField="VariableField1" HeaderText="VariableField1" AllowSorting="true" />

    </Columns>

    C#
    GridTableView detailsTable = (GridTableView)CampaignsRadGrid.MasterTableView.DetailTables[0].DetailTables[0];
    //foreach (GridColumn c in detailsTable.Columns)
    //{
    //    c.Display = true;
    //}
    GridColumn test = CampaignsRadGrid.MasterTableView.DetailTables[0].DetailTables[0].GetColumn("ID");
    test.Display = true;
    //CampaignsRadGrid.MasterTableView.GetColumn("VariableField1").Display = true;
    CampaignsRadGrid.Rebind();
    CampaignsRadGrid.ExportSettings.ExportOnlyData = true;
    CampaignsRadGrid.ExportSettings.IgnorePaging = false;
    CampaignsRadGrid.ExportSettings.OpenInNewWindow = false;

    any ideas on what to do to make those columns display in the Export?
  7. Peter Parsons
    Peter Parsons avatar
    14 posts
    Member since:
    Sep 2008

    Posted 23 Jul 2010 Link to this post

    protected void rgUserListing_ItemCommand(object source, GridCommandEventArgs e)
           {
               if (e.CommandName == "ExportToExcel")
               {
                   rgUserListing.ExportSettings.FileName = "UserList" + DateTime.Now.ToString("-dd-MM-yyyy");
                   foreach (GridColumn col in rgUserListing.MasterTableView.Columns)
                       col.Visible = true;
               }
           }
  8. Adam Hey
    Adam Hey avatar
    8 posts
    Member since:
    Apr 2010

    Posted 26 Jul 2010 Link to this post

    Thanks Anthony,
       I had already tried marking the columns as Visible=true and Display=true, but to no avail.
    In the end, I used a separate temporary (hidden) RadGrid for the export. Not ideal and not the most efficient solution, but time was against me...
  9. Daniel
    Admin
    Daniel avatar
    4949 posts

    Posted 29 Jul 2010 Link to this post

    Hello Adam,

    You can try to traverse the columns recursively:
    void ShowAllColumns(GridTableView gridTableView)
    {
        foreach (GridNestedViewItem nestedViewItem in gridTableView.GetItems(GridItemType.NestedView))
        {
            if (nestedViewItem.NestedTableViews.Length > 0)
            {
                foreach (GridColumn column in gridTableView.Columns)
                    column.Display = true;
                ShowAllColumns(nestedViewItem.NestedTableViews[0]);
            }
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        ShowAllColumns(RadGrid1.MasterTableView);
        RadGrid1.MasterTableView.ExportToExcel();
    }

    Regards,
    Daniel
    the Telerik team
    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 Public Issue Tracking system and vote to affect the priority of the items
  10. rahaf
    rahaf avatar
    5 posts
    Member since:
    Sep 2014

    Posted 06 Oct 2015 in reply to Adam Hey Link to this post

    Dear Adam, 

    kindly your advice about export detail table as you made before, just i want to know how to disable paging in detail table to export data when expand one record of main radgrid 

  11. rahaf
    rahaf avatar
    5 posts
    Member since:
    Sep 2014

    Posted 06 Oct 2015 in reply to rahaf Link to this post

    Sorry dear, but in other word i want to do the following: 

    When clicking export in the detail table, export all records of the detail table (for that master table record).

    any way to do that please

  12. Daniel
    Admin
    Daniel avatar
    4949 posts

    Posted 08 Oct 2015 Link to this post

    Hello Rahaf,

    Exporting separate table views is not recommended and might not work as expected. The export buttons are working this way only because we would like to avoid breaking changes.
    That said, if you let me know which export format is used in your project I may have a suggestion for that particular case.

    Regards,
    Daniel
    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
  13. rahaf
    rahaf avatar
    5 posts
    Member since:
    Sep 2014

    Posted 09 Oct 2015 in reply to Daniel Link to this post

    hello Daniel, 

    i want to export the data into csv file, i use the same way that used in the first post of this conversation that introduce the way of exporting detail table for one record of grid that we expand and if detail table for that records takes more than one page i can't export all pages in detail table for one record of detail table that we expanded, but i can export only one page of that detail table.

     

    please help me, just i want to export data of record from hierarchical radgrid that we expanded to csv file

  14. Daniel
    Admin
    Daniel avatar
    4949 posts

    Posted 14 Oct 2015 Link to this post

    Hello Rahaf,

    You can try the following approach:
    protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
     {
         if (e.CommandName == RadGrid.ExportToCsvCommandName)
         {
             RadGrid1.ExportSettings.IgnorePaging = false;
             e.Item.OwnerTableView.AllowPaging = false;
             e.Item.OwnerTableView.Rebind();
         }
    }

    I hope this helps.

    Regards,
    Daniel
    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
Back to Top