Telerik Radgrid loses paging when exporting to Excel

Asked

Viewed 63 times

0

When I export Radgrid from Telerik to Excel, when Radgrid has many records, it loses paging. I tried to solve with rebind, but it didn’t work. Would anyone have any idea?

Attached the screens before and after export to excel.Antes de exportar: After exporting to excel and clicking on one of the Paggings. The Paggings vanish.

Após exportar para excel.

Before trying the solution with rivend, it was more frequent this occur, now it occurs more often, sometimes it does not occur. Also, when I click on the other tabs, which also contain grids with records, when I export to excel, and give the rebind, it goes back to the main tab.

I would also like to know how to keep myself in the same tab where I exported the records to excel. When I am in the main tab, the other tabs are active only when I select a line, so they load with the line information. However, after the rebind, this information from the other tabs is lost.

Code in C#:

private void ClickToolbar(short value)
    {
        this.ExportSettings.HideStructureColumns = true;
        this.ExportSettings.FileName = string.Concat(this.ExportSettings.Pdf.PageTitle, " - Exportação");

        this.MasterTableView.HierarchyDefaultExpanded = false;
        this.MasterTableView.HierarchyLoadMode = GridChildLoadMode.ServerOnDemand;

        if (this.MasterTableView.HasDetailTables)
        {
            for (int i = 0; i < this.MasterTableView.DetailTables.Count; i++)
            {
                this.MasterTableView.DetailTables[i].HierarchyDefaultExpanded = false;
                this.MasterTableView.DetailTables[i].HierarchyLoadMode = GridChildLoadMode.ServerOnDemand;

            }
        }

        GridTableView backupMasterTableView = this.MasterTableView.Clone();

        switch (value)
        {
            case 0:
                GridSettingsPersister savePersister = new GridSettingsPersister(this);
                object[] parameters = new object[] { this.IdGridConfiguracao, savePersister.SaveSettings() };
                this.Page.GetType().GetMethod("SalvarConfiguracaoGrid").Invoke(this.Page, parameters);

                if (RebindOnClickSaveToolbar)
                    this.Rebind();
                break;
            case 2:
                exporting = true;
                PrepareToPrintGrid();
                this.MasterTableView.ExportToPdf();
                break;
            case 4:
                #region Formato do Excel


                this.ExportSettings.Excel.Format = Telerik.Web.UI.GridExcelExportFormat.Biff;
                #endregion

                PrepareToPrintGrid();
                exporting = true;
                this.MasterTableView.ExportToExcel();
                this.MasterTableView.Rebind();

                break;
        }

        if (exporting)
        {
            this.MasterTableView.HeaderStyle.Wrap = backupMasterTableView.HeaderStyle.Wrap;
            this.MasterTableView.ItemStyle.Wrap = backupMasterTableView.ItemStyle.Wrap;
            this.MasterTableView.Font.Size = backupMasterTableView.Font.Size;
            this.MasterTableView.Font.Name = backupMasterTableView.Font.Name;
            this.MasterTableView.Style["vertical-align"] = backupMasterTableView.Style["vertical-align"];
            this.MasterTableView.Style["text-align"] = backupMasterTableView.Style["text-align"];
            this.MasterTableView.UseAllDataFields = backupMasterTableView.UseAllDataFields;
            this.MasterTableView.HierarchyDefaultExpanded = backupMasterTableView.HierarchyDefaultExpanded;
            this.MasterTableView.HierarchyLoadMode = backupMasterTableView.HierarchyLoadMode;

            this.MasterTableView.PageSize = backupMasterTableView.PageSize;
            this.MasterTableView.AllowPaging = backupMasterTableView.AllowPaging;

        }
    }

protected void PrepareToPrintGrid()
    {
        #region Seleciona o tamanho do papel
        // Change to legal paper for high column counts
        if (this.MasterTableView.Columns.Count > 8)
        {
            this.ExportSettings.Pdf.PaperSize = GridPaperSize.Legal;
        }
        // Change to portrait if many columns
        if (this.MasterTableView.Columns.Count > 3)
        {
            Unit swapUnit = this.ExportSettings.Pdf.PageWidth;
            this.ExportSettings.Pdf.PageWidth = this.ExportSettings.Pdf.PageHeight;
            this.ExportSettings.Pdf.PageHeight = swapUnit;
            this.ExportSettings.IgnorePaging = true;
            this.ExportSettings.OpenInNewWindow = true;
        }
        #endregion

        #region Estilos
        this.ExportSettings.Pdf.DefaultFontFamily = "Segoe UI";
        this.MasterTableView.HeaderStyle.Wrap = true;
        this.MasterTableView.ItemStyle.Wrap = true;
        this.MasterTableView.Font.Size = 8;
        this.MasterTableView.Font.Name = "Segoe UI";
        this.MasterTableView.Style["vertical-align"] = "middle";
        this.MasterTableView.Style["text-align"] = "left";
        #endregion

        //this.ExportSettings.IgnorePaging = true;
        this.ExportSettings.IgnorePaging = true;
        this.ExportSettings.OpenInNewWindow = true;
        this.MasterTableView.UseAllDataFields = true;

        #region Expande Detailstable
        this.MasterTableView.HierarchyDefaultExpanded = true;
        this.MasterTableView.HierarchyLoadMode = GridChildLoadMode.Client;

        if (this.MasterTableView.HasDetailTables)
        {
            for (int i = 0; i < this.MasterTableView.DetailTables.Count; i++)
            {
                this.MasterTableView.DetailTables[i].HierarchyLoadMode = GridChildLoadMode.Client;
                this.MasterTableView.DetailTables[i].HierarchyDefaultExpanded = true;

            }
            this.MasterTableView.HierarchyDefaultExpanded = true;
        }
        #endregion
    }

Javascript code:

$(document).on('click', '.btn_toolbar_excel', function (e) {
    setTimeout(function () { preventTableBreakPager() }, 5000);
})

function preventTableBreakPager()
{
var gridVeiculosList = $find(gridVeiculos).get_masterTableView();
gridVeiculosList.rebind();
}
  • I don’t get it... Aren’t you filling out all the information for Excel when there are too many records? Or do you expect Excel to implement paging? Present the code...

  • It saves right. Only, grid paging is lost. When I click on the export button, I can save all the information. But after this procedure, when I click on one of the patterns, the grid is lost, there is only one pagination. I made several changes, before it was a long pagination with all the records, now only one with few records. I will post two prints before and after exporting to excel.

  • And the code of the method you export, where it is?

  • I posted up there.

1 answer

1

Your Javascript function preventTableBreakPager does not need to be called in the button click, because the Grid does not need to be updated.

I had the same problem with exporting Telerik to Excel in the past. The solution was to cancel the Grid update using event Onrequeststart of the component Radajaxmanager.

You need to declare the Radajaxmanager in his Page or Masterpage. You are probably already using this component:

 <telerik:RadAjaxManager ID="RadAjaxManager1" runat="server">
     <AjaxSettings>
         <telerik:AjaxSetting AjaxControlID="btn_toolbar_excel">
             <UpdatedControls>
                 <telerik:AjaxUpdatedControl ControlID="gridVeiculos" />
             </UpdatedControls>
         </telerik:AjaxSetting>
     </AjaxSettings>
 </telerik:RadAjaxManager>

You need to add this code to the event Load page:

protected override void OnLoad(EventArgs e)
{
   if (!IsPostBack)
   {
      var ajaxManager = RadAjaxManager.GetCurrent(this);
      if (ajaxManager != null)
      {
          // Está função está no arquivo Public.js
          ajaxManager.ClientEvents.OnRequestStart = "radAjaxManager_requestStart";
      }
   }
}

And finally, you need to add this Javascript code:

function radAjaxManager_requestStart(sender, args) {

    // Cancela a atualização do Ajax durante a exportação para Excel
    if (args.get_eventTargetElement().id.indexOf("btn_toolbar_excel") >= 0) {
        args.set_enableAjax(false);
    }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.