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
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.
}
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
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.
}
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
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.
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
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
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.