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

exports only the records shown in grid using GridViewSpreadStreamExport

8 Answers 59 Views
GridView
This is a migrated thread and some comments may be shown as answers.
KR
Top achievements
Rank 1
Veteran
KR asked on 01 Mar 2019, 06:43 PM

Hi

I'm trying to export ALL the records using GridViewSpreadStreamExport. Here is the snippet.

But the excel sheet is only showing the records displayed in the grid (which is 20). I have a RadDataPager and the is set to 20 with source ElementName as RadGridView.

How do I export all data in the Logs ItemSource of the grid?

 

<telerik:RadGridView  x:Name="rgvLogs"
         IsBusy= "{Binding IsBusy}"
         ItemsSource="{Binding Logs}"
         AutoGenerateColumns="True"
          IsReadOnly="True"
          ShowColumnSortIndexes="True"
          CanUserDeleteRows="False"
          CanUserInsertRows="False"
          CanUserSortColumns="True"
          CanUserFreezeColumns="False"
           GroupRenderMode="Flat"
           Grid.Row="1">      
  </telerik:RadGridView>
  <telerik:RadDataPager x:Name="radDataPager"
                                PageSize="20"
                                IsTotalItemCountFixed="True"
                                DisplayMode="All"
                                Grid.Row="2"
                                FontSize="12"
                                AutoEllipsisMode="Both"
                                NumericButtonCount="10"
                                Source="{Binding Items, ElementName=rgvLogs}"/>
01.private void AsyncExport(object param)
02. {
03.     string extension = "xlsx";
04.     var rgvExportToExcel = param as RadGridView;
05.
06.     var dialog = new SaveFileDialog()
07.     {
08.         DefaultExt = extension,
09.         Filter = String.Format("(*.{0})|*.{1}", extension, extension),
10.         FilterIndex = 1,
11.         AddExtension = true,
12.         Title = "Export to Excel",
13.     };
14.
15.     if (dialog.ShowDialog() == DialogResult.OK)
16.     {
17.         GridViewSpreadStreamExport spreadStreamXlsxExport = new GridViewSpreadStreamExport(rgvExportToExcel);
18.         spreadStreamXlsxExport.ShowLoadingIndicatorWhileAsyncExport = this.ShowLoadingIndicatorWhileAsyncExport;
19.         spreadStreamXlsxExport.RunExportAsync(dialog.FileName.ToString(), new SpreadStreamExportRenderer(),
20.                                                 new GridViewSpreadStreamExportOptions()
21.                                                 {
22.                                                     ShowColumnHeaders = true,
23.                                                     ShowColumnFooters = true,
24.                                                     ExportDefaultStyles = true
25.                                                 });
26.     }
27.
28. }

8 Answers, 1 is accepted

Sort by
0
KR
Top achievements
Rank 1
Veteran
answered on 05 Mar 2019, 03:31 AM

I have changed the above code to include a little logic that controls the PageSize before and after the SpreadStreamExport as below.

But the first time button click returns the records in the grid (20 rows), not all records. H, the 2nd button click returns all the records in . What am missing  Why is the export not exporting all records in the first attempt as set the PageSize to  DO I need to fire event manually? I could not find the event for RadDataPager.

 

01.private void AsyncExport(object param)
02.  {
03.      string extension = "xlsx";
04.      var args = (object[])param;
05.      // used a MultiParamConverter to pass grid and pager on button click
06.      var rgvExportToExcel = args[0] as RadGridView;
07.      var radDataPager = args[1] as RadDataPager;
08.
09.      var dialog = new SaveFileDialog()
10.      {
11.          DefaultExt = extension,
12.          Filter = String.Format("(*.{0})|*.{1}", extension, extension),
13.          FilterIndex = 1,
14.          AddExtension = true,
15.          Title = "Export to Excel",
16.      };
17.      if (dialog.ShowDialog() == DialogResult.OK)
18.      {
19.          int originalPageSize = radDataPager.PageSize;
20.          int originalPageIndex = radDataPager.PageIndex;
21.          radDataPager.PageSize = 0;
22.          rgvExportToExcel.Rebind();
23.          
24.          GridViewSpreadStreamExport spreadStreamXlsxExport = new GridViewSpreadStreamExport(rgvExportToExcel);
25.          spreadStreamXlsxExport.ShowLoadingIndicatorWhileAsyncExport = this.ShowLoadingIndicatorWhileAsyncExport;
26.          spreadStreamXlsxExport.RunExportAsync(dialog.FileName.ToString(), new SpreadStreamExportRenderer(),
27.                      new GridViewSpreadStreamExportOptions()
28.                       {
29.                               ShowColumnHeaders = true,
30.                               ShowColumnFooters = true,
31.                               ExportDefaultStyles = true
32.                        });
33.          radDataPager.PageSize = originalPageSize;
34.          radDataPager.PageIndex = originalPageIndex;
35.      }
36.  }
0
Dilyan Traykov
Telerik team
answered on 06 Mar 2019, 12:11 PM
Hello KR,

As you've correctly figured out, you need to set the PageSize of the RadDataPager to 0 prior to exporting the data to have all records exported rather than only the current page.

As the export process is asynchronous, however, returning the original PageSize should happen after the process is done, namely in the AsyncExportCompleted event handler:

private void SpreadStreamXlsxExport_AsyncExportCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
{
    radDataPager.PageSize = originalPageSize;
    radDataPager.PageIndex = originalPageIndex;
}

I've attached a sample project where doing so results in all the records being exported for your reference.

Please let me know whether you manage to achieve the same result.

Regards,
Dilyan Traykov
Progress Telerik
Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
0
KR
Top achievements
Rank 1
Veteran
answered on 07 Mar 2019, 03:22 AM

Hi Dilyan,

Your attached sample runs excellent. However, as soon as I added the AsyncExportCompleted event handler (line 23, 34 onwards), the screen just freezes. And the control never steps in the AsyncExportCompleted event.

I compared my code with the sample code and they look the same. Any idea what could cause this behavior?

 

01.private void AsyncExport(object param)
02.{
03.            string extension = "xlsx";
04.            var args = (object[])param;
05.            rgvExportToExcel = args[0] as RadGridView;
06.            radDataPager = args[1] as RadDataPager;
07.
08.            var dialog = new SaveFileDialog()
09.            {
10.                DefaultExt = extension,
11.                Filter = String.Format("(*.{0})|*.{1}", extension, extension),
12.                FilterIndex = 1,
13.                AddExtension = true,
14.                Title = "Export to Excel",
15.            };
16.            if (dialog.ShowDialog() == DialogResult.OK)
17.            {
18.                originalPageSize = radDataPager.PageSize;
19.                originalPageIndex = radDataPager.PageIndex;
20.                radDataPager.PageSize = 0;
21.
22.                GridViewSpreadStreamExport spreadStreamXlsxExport = new GridViewSpreadStreamExport(rgvExportToExcel);
23.                spreadStreamXlsxExport.AsyncExportCompleted += SpreadStreamXlsxExport_AsyncExportCompleted;
24.                spreadStreamXlsxExport.ShowLoadingIndicatorWhileAsyncExport = true;
25.                spreadStreamXlsxExport.RunExportAsync(dialog.FileName.ToString(), new SpreadStreamExportRenderer(),
26.                    new GridViewSpreadStreamExportOptions()
27.                    {
28.                       ShowColumnHeaders = true,
29.                       ShowColumnFooters = true,
30.                       ExportDefaultStyles = true
31.                    });
32.            }
33.        }
34.        private void SpreadStreamXlsxExport_AsyncExportCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
35.        {
36.            radDataPager.PageSize = originalPageSize;
37.            radDataPager.PageIndex = originalPageIndex;
38.}
0
KR
Top achievements
Rank 1
Veteran
answered on 07 Mar 2019, 05:30 AM
and it also looks like the excel file is created but unable to open it as it throws the attached error. 
0
Dilyan Traykov
Telerik team
answered on 08 Mar 2019, 03:41 PM
Hi KR,

I'm afraid I'm unable to pinpoint a cause for this behavior by only looking at the code you provided. Indeed, the two code snippets seem identical.

Would you find it possible to isolate the behavior you observe in a small sample project so that I can investigate it at my end and assist you further? Please note that as the forum system does not accept attachments other than images, you will either need to open a new support ticket through an account with an active license, or upload the project in a storage provider of your choice.

Thank you in advance for your cooperation on the matter.

Regards,
Dilyan Traykov
Progress Telerik
Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
0
KR
Top achievements
Rank 1
Veteran
answered on 08 Mar 2019, 07:02 PM

Hi Dilyan,

I think I figured the reason why the UI hangs once clicked on export. To reproduce this, In the sample project you provided in the earlier post, change the below likes from

<Grid.RowDefinitions>
    <RowDefinition Height="*"/>
    <RowDefinition Height="Auto" />
    <RowDefinition Height="Auto" />
</Grid.RowDefinitions>

 

to

<Grid.RowDefinitions>
    <RowDefinition Height="Auto"/>
    <RowDefinition Height="Auto" />
    <RowDefinition Height="Auto" />
</Grid.RowDefinitions>

 

Changing the RowDefinition from * to Auto hangs the UI and gets struck some kind of loop.

 

0
Dilyan Traykov
Telerik team
answered on 11 Mar 2019, 02:22 PM
Hi KR,

Indeed, this explains the hang as when the RadGridView control is placed in a row definition with height Auto it is measured with infinity, which breaks the control's UI virtualization. Once the PageSize is set to 0, all rows will be attempted to be drawn which will block the UI for a certain period, depending on the number of items in the bound collection.

The solution would be to place the control in a panel which does not measure it with infinity. Please let me know whether you find this possible.

Regards,
Dilyan Traykov
Progress Telerik
Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
0
KR
Top achievements
Rank 1
Veteran
answered on 11 Mar 2019, 02:42 PM
yes. I changed the  Height from Auto to *.
Deltaohm
Top achievements
Rank 2
Iron
Iron
commented on 03 May 2022, 09:23 AM

Hi, I have the same problem.
My grid is inside RadDocking.DocumentHost -> gird(height=*) ->grid(height=*)
After setting pagesize = 0, the call to ExportToWorkbook hangs: I have ~ 176000 rows, in debug I see that the cpu is all for devenv process, not for my application.
Thank you

Luigi

Dilyan Traykov
Telerik team
commented on 04 May 2022, 11:01 AM

Hi Luigi,

To test this, can you set the MaxHeight property of the RadGridView control to an arbitrary value (let's say 500)? If this resolves the hang, most probably the RadGridView is still measured with Infinity, despite the fact that the DocumentHost has its Grid.Height set to *.

If setting the MaxHeight does resolve the issue, please share the XAML layout in which the RadGridView is placed so that I can test this at my end and try to suggest a viable solution.
Tags
GridView
Asked by
KR
Top achievements
Rank 1
Veteran
Answers by
KR
Top achievements
Rank 1
Veteran
Dilyan Traykov
Telerik team
Share this question
or