Exploration of New Methods in Crystal Reports WPF Viewer
 
Published: 04 Jan 2011
Abstract
This article delves into the new methods exposed in Crystal Reports new WPF Viewer. These methods include the ReportDocument.BeforeFormatPage Event, the ReportRequestStatus.NumberOfPages, and more.
by Eric Landes
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 25742/ 44

Introduction

Crystal reports, long a Visual Studio add in staple, is no longer included out of the box with VS 2010.  Traditionally Crystal Reports has been the reporting tool of choice included with VS.  While the Crystal Report engine is not included with VS 2010, there is still a CR template included.  This template stubs your project as a Crystal Reports project.  You can then obtain the new CR for VS 2010 report engine by downloading it free of charge from the SAP website.  Currently that link is here

This download includes a new WPF viewer for Windows Forms that has some nifty new events to program against.  For information on non-coding aspects of the new viewer, please check out this article. 

Download

[Sample Download]

New API features

The following are some new features you can find in the API.  This listing is from the final release. 

New Formatting events (report cannot be modified during formatting event)

·         ReportDocument.BeforeFormatPage event

·         ReportDocument.AfterFormatPage event

·         ReportDocument.FieldMapping event

·         ReportDocument.NoData event

Check Report Processing Status

·         ReportDocument.ReportRequestStatus.NumberOfPages

·         ReportDocument.ReportRequestStatus.NumberOfRecordsRead

·         ReportDocument.ReportRequestStatus.NumberOfRecordsSelected

For this article we will focus on the Report Processing Status properties listed above.  This should give you an idea of where you can utilize these new properties, and where they are not useful.

Installing the Latest Bits

To get the latest version of Crystal for Visual Studio 2010, you need to download it from the SAP site.  Keep in mind that now Crystal does not come directly bundled with VS 2010, but requires a download.

Visual Studio 2010 comes with a template for Crystal.  You are prompted to download Crystal Reports for Visual Studio 2010 from the SAP website.  Here is the link

Open the downloaded files from SAP and either double click on the CRforVS_13_0 file or use the run command line.  Follow the on screen instructions.  Select the "Standard EXE installation package which installs the software into the Visual Studio IDE" option.  This is the full version and should install everything you need to create Crystal Report Projects in Visual Studio.

Creating a new Reporting Application with the WPF Viewer

To create a new project, select the "Crystal Reports WPF Application" from the new projects menu.  It can be found under the Reporting category of your installed templates.  After creating the project, the new solution has a WPF class, and a Crystal Report should be visible in the Solution Explorer.  That WPF file has a WPF Crystal Viewer bound to it.  Let me warn you that creating a WPF solution from Visual Studio templates then trying to add the Crystal viewer to that solution is tricky to implement.  It is much easier to just use the templates provided from SAP.

The new WPF viewer control to use in your windows applications is a slick new control.  It looks better and has some newer features, as revealed in the API section above.  Binding a report to the viewer is done most easily programmatically. 

If you want to allow the end user some control of what report to view, enable the property ShowOpenFileButton to allow the user to select a report.  Eventually there will be a property to set this, but you can set the report to an embedded resource using code.  For instance, in example 1, the code shows how to bind an embedded resource to the report viewer. 

CrystalReport1 is a report created in a project.  To bind this report to the new WPF viewer, use the property ViewerCore.ReportSource and set it to the instance of the embedded report.  Code Sample 1 shows the code.

Code Sample 1

            InitializeComponent();
            reportViewer.Owner = this;
            CrystalReport1 newCR = new CrystalReport1();
            reportViewer.ViewerCore.ReportSource = newCR;

Once you have set the code to bind this report to your viewer, you are now ready to use some of the new WPF properties.  Let's go through each property and find the methods where it is best to use them.

NumberOfPages

Once you have set up a CR Project and created the initial code to bind your report, you can now start experimenting with the new API methods and events available to you.  All of these properties are from the ReportRequest class and will return some data available from the Report document being rendered.  In this case we start with ReportDocument.ReportRequestStatus.NumberOfPages

In working with this method, it appears that until the report has been renedered, this method will only return a 0.  Once rendered, then the number of pages in total that the report has will be returned. 

For example, the code displayed in the LayoutUpdated method of the viewer (see this in code example 2), fires multiple times during startup.  Hovering over this method in debug mode until the viewer renders the report, we see that the count is 0.  This can be a useful way to programmitcally get page numbers from  reports with large datasets that vary.

Code Sample 2

Private void reportViewer_LayoutUpdated(object sender, EventArgs e)
{
      Int numPages = newCR.ReportRequestStatus.NumberOfPages;

}

Use this after LayoutUpdated, or Refresh to determine how many pages are now displaying in the viewer.

NumberOfRecordsRead

This property can be used, in the event LayoutUpdated, and is populated after parameters are filled out.  Other events where this can be useful are in the Refresh, and RequestBringIntoView event. 

As noted above, the LayoutUpdated method fires multiple times during loading of the report.  Only after the parameter is selected have I found that this property is populated.  Since the Viewer event Loaded fires before parameters are selected, this property is not populated during that event.

This property can be useful during events like RequestBringIntoView, and Refresh also.  Any event that fires after these should also be able to utilize this property.  This could be helpful during backend processing, if you are writing logs, or if any kind of error is encountered to help troubleshoot those kind of issues.

Code Sample 3

Private void reportViewer_LayoutUpdated(object sender, EventArgs e)
{
      Int numPages = newCR.ReportRequestStatus.NumberOfPages;
      Int numRecordsRead = newCR.ReportRequestStatus.NumberOfRecordsRead;
}

NumberOfRecordsSelected

This property is very similar to NumberOfRecordsRead.  In fact in the course of this article, the results from this property were not different than that of NumberOfRecordsRead for the Refresh, RequestBringIntoView, Loaded, SelectionChanged and LayoutUpdated methods. While there may be a difference and a good place to utilize this, it is not within these methods. 

Below is some sample code for how you can utilize this property.  It is very similar to NumberOfRecordsRead.

Code Sample 4

Private void reportViewer_SelectionChange(object sender, RoutedEventArgs e)
{
      Int numRecordsSelecteds = newCR.ReportRequestStatus.NumberOfRecordsSelected;

}

Summary

Above we have seen where some of these new WPF events can be used to gather information programmatically for your Crystal Reports .NET applications.  There are other areas that are new as well, and I encourage you to experiment with these new methods and share with the Crystal community.

Remember keep coding, and keep on creating great reports.



User Comments

No comments posted yet.






Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-03-29 9:04:01 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search