Delivering Reports with the Windows Forms Viewer

After reports are imported into or referenced from a Visual Studio .NET project, the next obvious step is to have a way to view those reports. This section covers report viewing in Windows Forms applications.

Windows Forms is the new .NET technology for building rich-client applications. It is the evolution of the COM and ActiveX platform that Crystal Reports was so popular in. When it comes to .NET, Crystal Decisions decided to write a native .NET control based on the Windows Forms technology. This control is simply called the Windows Forms Viewer. Its corresponding class name is CrystalDecisions.Windows.Forms.CrystalReportViewer.

Like other Windows Forms controls, this control ultimately inherits from the System.Windows.Forms.Control class. It has many public methods and properties that enable you to drive the appearance and behavior. In addition to these runtime capabilities, the Windows Forms Viewer has design-time support to increase the efficiency and ease of using the control. The control can be found in the toolbox on the Windows Forms tab. You can see what the control looks like after being dropped onto a form in Figure 29.3.

Figure 29.3. A Crystal Report displayed in the Windows Forms Viewer control.


The ReportSource Property

Although there are many properties and methods, the ReportSource property is key. It is this property that is used to indicate to the viewer which report it should display. Because the ReportSource propertys data type is object, it can accept multiple types of values, the most common of which are listed here:

  • Filename: The full path to an RPT file as a String object.
  • Report object: An instance of a CrystalDecisions.CrystalReports. Engine.ReportDocument class. This report should already have been loaded by calling the ReportDocuments Load method.
  • Strongly typed report object: An instance of a strongly typed report object derived from ReportClass.

The following code shows VB.NET examples of setting these types of report source objects:

 #1  A filename as a string

Viewer.ReportSource = "C:Program FilesMy ApplicationReportsSales.rpt"

 #2  a ReportDocument object

Dim Report = As New ReportDocument()

Report.Load("C:Program FilesMy ApplicationReportsSales.rpt")

Viewer.ReportSource = Report

 #3  A strongly-typed report object

Dim Report As New SalesReport()

Viewer.ReportSource = Report

If the viewer is visible when the ReportSource property is set, it displays the report immediately. If the viewer is not visible yet, that is, the form has not been shown yet, the viewer waits until it is shown onscreen to display the report. After a report source is provided to the viewer, it maintains that report until the viewer is destroyed or another report source is passed into it.

Because the viewer can generically accept report filenames and report objects, a single viewer can be reused for viewing multiple reports. One of the ways you could handle this is to create a form dedicated to report viewing. This form would contain the Windows Forms viewer. To easily invoke this form and pass in a report source, make the viewer a public variable and then create a shared method to accept a report source as an argument. An example of this function is shown here:

Public Shared Sub Display(ByVal ReportSource As Object)

 Dim newForm As New ReportViewerForm()

 newForm.Viewer.ReportSource = ReportSource


End Sub

After this is in place, to invoke the report viewer from anywhere in the application, use the following code:

strReportPath = ...


Customizing the Windows Forms Viewer

There are many properties and methods of the report viewer that can be used to customize its appearance. The first level of customization is to show or hide the individual components of the viewer. The group tree on the left side can be shown or hidden via the DisplayGroupTree Boolean property. The toolbar works the same way via the DisplayToolbar property.

In addition to hiding the entire toolbar, each button or button group on the toolbar has corresponding properties that allow them to be individually hidden or shown. These properties can be found in the property browser or accessed via code. They all start with Show, such as ShowExportButton, ShowPrintButton, and so on. The names should be self-explanatory.


There is a status bar at the bottom of the viewer that does not have a corresponding show/hide property. It tends to not add a lot of value and end up more of an annoyance than anything. A trick to hide this status bar is to drop a panel control onto the form and drop the viewer onto the panel. Set the viewers Dock property to Fill so that the viewer always sizes itself to the size of the surrounding panel. Then set the DockPadding.Bottom property of the viewer to -20. This sizes the height of the viewer to 20 pixels more than the panel, effectively hiding the status bar below the extents of the panel. Keep in mind that any methods and properties that need to be accessed from the report viewer after youve done this need to be accessed via the panel objects controls collection. Figure 29.4 shows the report viewer with no group tree, no toolbar, and the status bar hidden.

Figure 29.4. The Report Viewer is shown here with its status bar hidden.


Another property that can be used to change the behavior of the report viewer is the EnableDrillDown property. Setting this Boolean property to false disables the user from performing any drill-down operations. Finally, the SelectionFormula and ViewTimeSelectionFormula properties can be used to create and append filters to the report. Keep in mind that the filtering is actually done by the report engine, but the report viewer simply exposes the property and then sends the information down to the report engine. The SelectionFormula property should be used when creating or overwriting a selection formula. To append to an existing formula, use the ViewTimeSelectionFormula property, which automatically appends using an AND operator.

Part I. Crystal Reports Design

Creating and Designing Basic Reports

Selecting and Grouping Data

Filtering, Sorting, and Summarizing Data

Understanding and Implementing Formulas

Implementing Parameters for Dynamic Reporting

Part II. Formatting Crystal Reports

Fundamentals of Report Formatting

Working with Report Sections

Visualizing Your Data with Charts and Maps

Custom Formatting Techniques

Part III. Advanced Crystal Reports Design

Using Cross-Tabs for Summarized Reporting

Using Record Selections and Alerts for Interactive Reporting

Using Subreports and Multi-Pass Reporting

Using Formulas and Custom Functions

Designing Effective Report Templates

Additional Data Sources for Crystal Reports

Multidimensional Reporting Against OLAP Data with Crystal Reports

Part IV. Enterprise Report Design Analytic, Web-based, and Excel Report Design

Introduction to Crystal Repository

Crystal Reports Semantic Layer Business Views

Creating Crystal Analysis Reports

Advanced Crystal Analysis Report Design

Ad-Hoc Application and Excel Plug-in for Ad-Hoc and Analytic Reporting

Part V. Web Report Distribution Using Crystal Enterprise

Introduction to Crystal Enterprise

Using Crystal Enterprise with Web Desktop

Crystal Enterprise Architecture

Planning Considerations When Deploying Crystal Enterprise

Deploying Crystal Enterprise in a Complex Network Environment

Administering and Configuring Crystal Enterprise

Part VI. Customized Report Distribution Using Crystal Reports Components

Java Reporting Components

Crystal Reports .NET Components

COM Reporting Components

Part VII. Customized Report Distribution Using Crystal Enterprise Embedded Edition

Introduction to Crystal Enterprise Embedded Edition

Crystal Enterprise Viewing Reports

Crystal Enterprise Embedded Report Modification and Creation

Part VIII. Customized Report Distribution Using Crystal Enterprise Professional

Introduction to the Crystal Enterprise Professional Object Model

Creating Enterprise Reports Applications with Crystal Enterprise Part I

Creating Enterprise Reporting Applications with Crystal Enterprise Part II

Appendix A. Using Sql Queries In Crystal Reports

Creating Enterprise Reporting Applications with Crystal Enterprise Part II

Special Edition Using Crystal Reports 10
Special Edition Using Crystal Reports 10
ISBN: 0789731134
EAN: 2147483647
Year: 2003
Pages: 341 © 2008-2020.
If you may any questions please contact us: