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

Problem getting the ReportViewerModel after upgrade to Q2'11

3 Answers 108 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Pawel
Top achievements
Rank 1
Pawel asked on 02 Aug 2011, 02:26 PM
After upgrading to q2 release (for Silverlight)  i can't get ReportViewerModel from ReportViewer.

I tried to use 

var reportViewerModel = reportViewer.DataContext as ReportViewerModel;


but datacontext for this control i always null,

then I tried 

var layoutRoot = (FrameworkElement)VisualTreeHelper.GetChild(this.reportViewer, 0);
var rvm = (ReportViewerModel)(layoutRoot.DataContext);


this gives me an error :

Specified argument was out of the range of valid values.
Parameter name: childIndex


I found that before i could use ReportViewerModel constructor. After upgrade i cannot.

I'm trying to use PrintDocumentCommand. Can i do that without ReportViewerModel?

3 Answers, 1 is accepted

Sort by
0
Steve
Telerik team
answered on 04 Aug 2011, 07:28 AM
Hi Pawel,

The only reason for this problem would be if the report viewer template has not yet been created e.g. you're invoking the extension method in the window/user control constructor. To make sure the template is available use OnApplyTemplate e.g.:
Copy Code
public partial class MainPage : UserControl
   {
       public MainPage()
       {
           InitializeComponent();
       }
 
       public override void OnApplyTemplate()
       {
           base.OnApplyTemplate();
           this.ReportViewer1.PrintTheReport();
       }
   }
 
   static class ReportViewerExtensions
   {
       public static void PrintTheReport(this ReportViewer reportViewer)
       {
           var layoutRoot = (FrameworkElement)VisualTreeHelper.GetChild(reportViewer, 0);
           var viewerModel = (ReportViewerModel)layoutRoot.DataContext;
           var printCommand = viewerModel.PrintReportCommand;
           if (printCommand.CanExecute(null))
           {
               printCommand.Execute(null);
           }
       }
   }

Kind regards,
Steve
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get now >>

0
Pawel
Top achievements
Rank 1
answered on 04 Aug 2011, 07:41 AM
Thanks for repply.

Unfortunately I can't use OnApplyTemplate event because i don't show this control to user, so the event don't even fire.

I want to print report  in the background (without showing any reportviewer)
0
Steve
Telerik team
answered on 05 Aug 2011, 03:03 PM
Hi Pawel,

Currently you cannot print programmatically without the viewer and this would not change. You are not bound to use the OnApplyTemplate, we just gave it as example. You can invoke the PrintTheReport() extension method from a button click event.

All the best,
Steve
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get now >>

Tags
General Discussions
Asked by
Pawel
Top achievements
Rank 1
Answers by
Steve
Telerik team
Pawel
Top achievements
Rank 1
Share this question
or