Windows Forms Viewer


When integrating a Crystal report into a Windows application with VS.NET, you should first grow familiar with the separation between the actual report itself (the .RPT file you either create within the VS.NET IDE or add to it) and the Windows viewer that actually shows the report from within your application. If you are going to include a Crystal report in a Windows form as part of your application, you ll need to pay close attention to these two distinct objects. If, however, you only wish to include a report in an application for the purpose of exporting to a file location, printing to a local printer, or e-mailing as an attachment, then you may not even need to add a viewer that presents the report in a Windows form.

However, since many Windows applications that include a Crystal report will probably need to present the report to a user in a form, you ll immediately need to determine how to present the report from within a standard VS.NET Windows form. For this, VS.NET includes the Crystal Windows Forms Viewer. By dropping a Crystal Windows Forms Viewer onto a Windows form, you can immediately display a Crystal report in a form, including all embedded formatting. This viewer also supports all typical Crystal Reports interaction, including page-on-demand viewing, group tree navigation, report group drill-down, on-demand subreport interactivity, and report exporting and printing.

After creating a Windows application (the language you use to create it is irrelevant ” Crystal Reports is entirely language independent in VS.NET), add a Windows Forms Viewer to the desired Windows form by dragging it from the Windows Forms section of the Toolbox. The object, labeled CrystalReportsViewer in the toolbar, can then be resized once it appears on the form. You ll also find a set of properties specific to the Windows Forms Viewer in the Properties box. This is shown in Figure 28-1.

click to expand
Figure 28-1: Crystal Windows Forms Viewer in a Windows form

However, merely adding a Windows Forms Viewer to a Windows form won t display anything when the application is run. This is because, as you may gather from the discussion earlier in the chapter on the two distinct parts of a VS.NET Crystal Reports application, you must tie or bind an actual Crystal Report object to the Windows Forms Viewer so that there s an actual report to view.

Note  

Crystal Reports can be integrated in VS.NET applications based on Visual Basic, C#, Managed C++, or other languages supported in the VS.NET development environment. This chapter, however, will concentrate on Visual Basic applications and will demonstrate Visual Basic coding techniques. The material assumes you already have a fundamental knowledge of Visual Studio .NET architecture and coding techniques. This portion of the book will not teach you VS.NET fundamentals.

Windows Forms Viewer Binding Options

There are numerous ways to bind reports to the Windows Forms Viewer. The option you choose depends largely on the way your application is designed; whether your reports are external to your application (located on shared network server at a common file location for use by multiple applications), designed within the VS.NET IDE, contained in a Report Component (discussed later), and so forth.

Probably the simplest way to bind a report to the viewer is to specify the full pathname and filename of an .RPT file located on the local or network drive. There are several ways to accomplish this:

  • Specify the viewer s ReportSource property Make sure the Crystal Windows Forms Viewer is selected in the VS.NET IDE. Navigate to the ReportSource property in the Properties box. Click the down arrow and choose the Browse option. A file open dialog will appear. Navigate through drives and folders until you find the .RPT file you wish to display in the viewer.

  • Set the viewer s ReportSource property in code The report can be bound at run time by specifying the Crystal Windows Forms Viewer s ReportSource property. For example, if you display the code window for the Windows form containing the viewer (perhaps the form s Load event), specify the viewer s ReportSource property as follows :

     Private Sub Page_Load(ByVal sender As System.Object, _ 
    ByVal e As System.EventArgs) Handles MyBase.Load
    'Put user code to initialize the page here
    CrystalReportViewer1.ReportSource = "C:\Temp\FedEx Orders.rpt"
    End Sub

When you specify a report source, the Windows Forms Viewer won t show the report at design time. However, when you run the Windows application, the Windows form will display the report inside the Windows Forms Viewer within the form (you may be prompted for database credentials or parameter values first).

click to expand
Tip  

Two Crystal Windows Forms Viewer properties you ll want to acquaint yourself with are Anchor and Dock. These properties will allow you to attach the Windows Form Viewer to its parent Windows form on any combination of sides. If you plan on having the report fill the entire Windows form (this is often the most desirable choice), anchor or dock the Windows Forms Viewer on all four sides. The report viewer will resize inside the Windows form as you resize it.

You may also want to bind a report you ve created within the VS.NET IDE to a report viewer (the Integrated Report Designer is discussed later in the chapter, under Integrated Report Designer ). Or, you may wish to add an existing .RPT file to your web project and bind it to a viewer. These types of reports that reside in your project are known as strongly typed reports, and a class is added to the project for each strongly typed report. In these cases, you ll eventually see an .RPT filename in the Solution Explorer that represents the strongly typed report you want to bind to the viewer. Surprisingly, though, if you attempt to set the viewer s ReportSource property to the .RPT filename, you won t find the file in the drop-down list of available options in the Properties box.

This strongly typed report can be bound using a single line of code in the Windows form s Load event. By setting the viewer s ReportSource property to a new instance of the desired report class (sans the .RPT extension), you can bind the viewer to the report included in the project.

 'Binds strongly-typed FedEx Orders.RPT file from Solution Explorer 
CrystalReportViewer1.ReportSource = New FedEx_Orders

You may also wish to bind a report to the Crystal Windows Forms Viewer via strongly typed report component. In this situation, you add an additional component to the Windows form from the Toolbox and choose an existing strongly typed report in the project for the component to host. Begin by dragging the ReportDocument from the Components area of the Toolbox to the Windows form. Once you ve dropped it on the form, the Choose a ReportDocument dialog box will appear. Click the drop-down list and choose the desired report from the list (you ll see strongly typed reports you ve already added to your project).

click to expand

Once you click OK, the report component will appear in a design time “only area of the Windows form (it will not be visible at run time). You may then merely select the Crystal Windows Forms Viewer in the form and click the drop-down list for the ReportSource property in the Properties box. You ll see all report components you ve added to the form in the list. Choose the desired component.

Tip  

There are yet additional ways of binding reports to viewers. A complete discussion of all Windows Forms Viewer binding options, including sample code, can be found in VS.NET help. Search for Report Binding Options for Windows Forms Viewers . There are also samples and examples of report binding that can be downloaded from support.BusinessObjects.com or www.BusinessObjects.com/DevZone.




Crystal Reports 10
Crystal Reports 10: The Complete Reference
ISBN: B005DI80VA
EAN: N/A
Year: 2004
Pages: 223
Authors: George Peck

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net