And i started getting an error while trying to print.
Here is my code, which i have pretty much taken from the sample:
private
bool
CanPrint(
object
param)
{
return
true
;
}
private
void
Print(
object
param)
{
RadGridView grid = (RadGridView)param;
RadRichTextBox rtb =
new
RadRichTextBox() { Height = 0 };
rtb.Name =
"RadRichTextBox1"
;
Grid parent = grid.ParentOfType<Grid>();
if
(parent !=
null
&& parent.FindName(rtb.Name) ==
null
)
{
parent.Children.Add(rtb);
rtb.ApplyTemplate();
}
rtb.Dispatcher.BeginInvoke((Action)(() =>
{
rtb.Document = CreateDocument(grid);
}));
rtb.Print(
"MyDocument"
, Telerik.Windows.Documents.UI.PrintMode.Native);
}
private
RadDocument CreateDocument(RadGridView grid)
{
List<GridViewBoundColumnBase> columns = (from c
in
grid.Columns.OfType<GridViewBoundColumnBase>()
orderby c.DisplayIndex
select c).ToList();
Telerik.Windows.Documents.Model.Table table =
new
Telerik.Windows.Documents.Model.Table();
RadDocument document =
new
RadDocument();
Telerik.Windows.Documents.Model.Section section =
new
Telerik.Windows.Documents.Model.Section();
section.Blocks.Add(table);
document.Sections.Add(section);
AddDataRows(table, grid.Items, columns, grid);
return
document;
}
I did get this error once before when i had a breakpoint in the code before i upgraded from Q3 release.
But now that i am using Q1 2012 release i get this error every time.
My code is in a viewmodel.
11 Answers, 1 is accepted
The problem you are seeing resides in the very core of Silverlight. In Silverlight you can perform certain actions only when they are user-initiated (that would mean on a button click for example). Here you are opening a save file dialog when a button is clicked which is correct. But there is one more thing. Silverlight has a timeout limit during which the dialog must be shown. If that doesn't happen, a SecurityException is thrown.
The thing is your grid is too big so the creation of a RadDocument containing all information takes too long and the dialog tries to open too late when this timeout has already been exceeded.
You can create two buttons for example - one to prepare the document and one to print it. We know this is uncomfortable and not so user friendly but unfortunately is the only way to overcome this Silverlight limitation.
Please revise and get back to us with your opinion.
Andrew
the Telerik team
Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>
I tested this theory by removing all the code that builds the table, in fact all the code except the call to print the document.
In this case an empty document. The error still occurs. It happens virtually instantaneously after i press the button.
My new code:
RadRichTextBox rtb =
new
RadRichTextBox() { Height = 0 };
rtb.Name =
"RadRichTextBox1"
;
rtb.Document =
new
RadDocument();
rtb.Print(
"MyDocument"
, Telerik.Windows.Documents.UI.PrintMode.Native);
I am afraid that your theory does not appear to apply here. There must be another reason for this error.
I checked your account to see what version you previously worked with. It turns out we have added a lot of features since then and that has slowed down the initialization of RadRichTextBox.
As a workaround we would suggest initializing and adding the RadRichTextBox to the visual tree somewhere else (in the constructor of the page for example) and only setting the Document in the button click event handler. However, it is possible that this approach also results in the same exception, if the document is too big and its creation and layout require more time than the limit of the print dialog.
Andrew
the Telerik team
Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>
The error occurs because you instantiate the RadRichTextBox (which takes quite some time), assign a document (which takes time too) and if the document is big that takes additional time.
In order to ensure that the timeout of the PrintDialog will not be exceeded, you can create a print-preview like window with RadRichTextBox and a Print button in it. When the user decides to print the grid view and clicks a button, build the document and show in in a window with RadRichTextBox. You can have a button in this window which invokes only the print command. In this way, the successful printing of the document would be guaranteed.
I hope you find this solution acceptable.
Andrew
the Telerik team
Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>
Is there any fix to this issue in a later version ?
I'v downloaded the last version, and i get the same error.
(my code was copied from your demos site: http://demos.telerik.com/silverlight/#GridView/PrintAndExportWithRadDocument))
Also didn't succeed to print from the online demo itself.
ANY solution to that ??
the only print that worked for me is to use PrintToHtml method that doesn't seem to allow any styling at all.
Thanks,
Gitit
The discussed issue is related to a limitation in the framework and we are not aware of any released fix for it. At this the only workaround of this limitation is the one suggested below by Andrey.
I tested our demos and couldn't reproduce the described behavior - the document is printed as expected. Are you testing with the same data or with another? Could you give us exact steps to reproduce this in order to test on our end and check if there is something else preventing you from printing?
Regards,
Tanya
Telerik
Thank you for your reply.
I'm sorry to hear that there is no fix to this problem.
Meanwhile I'v succeeded to print only grid view with several rows (The grid view contains 20 columns).
I guess I'll have no choise but to implement the suggested solution.
Thanks,
Gitit
My gridview contains row details that has child gridview.
How can I print my grid including its row details ?
No matter what I do I wasn't able to print it, only the parent grid.
I'll be glad to hear if you have any solution.
Thanks,
Gitit
I'd suggest adopting the approach demonstrated in this online example which utilizes RadDocument. You can subscribe to the ElementExporting event of RadGridView when exporting the document, iterate the nested RadGridViews and export them separately. You can also take a look at this demo showing export of RowDetails.
I hope this is useful.
Regards,
Petya
Telerik