Crystal Reports Online Training

Learn Online, Anytime, Anywhere

Step-by-step online tutorials.

14.02 Steps for Using Reports

Basic Customization

No matter what type of runtime customization you want to perform, customizing reports always starts with the same premise: declare and instantiate a ReportDocument object, modify its properties, and print or preview it. These steps are discussed in more detail next.

As discussed in Chapter 3, Windows applications allow you to use either embedded or non-embedded reports. ASP.NET applications only allow non-embedded reports. The code listings in this chapter will give examples of how to use both types of reports. In the majority of cases, non-embedded reports will be used since they are available for both Windows and ASP.NET applications, but you can always use embedded reports if you are developing a Windows application.

Step 1: Declare and instantiate the ReportDocument object variable.

When using non-embedded reports, you have to declare and instantiate an object variable derived from the ReportDocument class. The report is loaded into memory by calling the Load() method of this object.

If you are using an embedded report in a Windows application, a class that has the same name as your report was automatically created in your project when you created the report. You can instantiate this class directly and it loads the report into memory. There is no need to call the Load() method.

In the examples throughout this chapter and the rest of the book, the report name being referenced is called CrystalReport1. This is the default report name that is given by the report wizard. When you implement the sample code in your applications, replace the name CrystalReport1 with the name of the report you want to print. I also name the report object variables ‘myReport’. When using the code listings in your own application, replace this name with a variable name that best describes your report.

Step 2: Modify the properties of the report object.

After instantiating the report object, all the properties and methods of the ReportDocument object are available to you. Set the properties that need to be modified. The different properties of the report object are explained throughout all the chapters in Part II of this book.

If you only want to print the report without changing any of the report properties, you can skip this step. The following examples skip this step because it is covered in extensive detail throughout the rest of this book.

Step 3: Preview or print the report.

To preview the report, assign the report object variable to the viewer’s ReportSource property. This automatically displays it to the user and lets them use the toolbar to navigate through the pages, export the report to an external file, or send it to the printer. If you want to print a report without previewing it, call the PrintToPrinter() method, discussed in the next section.

The following sections are sample code for previewing reports using a Windows client.

All the reports in this chapter assume that the report is not connecting to a secure data source. Passing login credentials is much more involved and has an entire chapter dedicated to it. See Chapter 17 for more information.