RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


The Baker's Dozen: 13 Productivity Tips for Crystal Reports Redux : Page 3

This article is a redux, or "Version 2.0" of the Baker's Dozen article on Crystal Reports that ran in the January/February 2005 issue of CoDe Magazine. The article updates some of the classes, covers some new features in Crystal Reports XI, and demonstrates techniques to help developers get the most out of Crystal Reports.

Tip 3: Building a Reusable Library for Common Reporting Tasks
I've incorporated a handful of reports into my application and find that I'm repeating common tasks for setting the report's data source, previewing, printing, and exporting the reports. I'd like to build and use a generic set of classes to handle all of these tasks every time.

Figure 10: The Crystal Reports tools project from the Common Ground Framework.
In my original Crystal Reports article, I presented a project called ccCrystalTools that contained some reusable functionality for previewing and printing reports. I've modified and enhanced the library since then. The library provides functionality for many common reporting tasks. The main benefit is that you don't have to repeat code for using the Crystal Reports object model.

Figure 10 shows the Visual Studio 2005 project for the library, now called CGS.CrystalReportsTools. The library consists of the following:

  • ccCrystalManager: a class that contains the core functionality for pushing datasets into reports, as well as previewing, printing, and exporting reports. Table 1 and Table 2 list the public methods and properties for this class.
Table 1: Public methods for ccCrystalManager parameters are in bold.
Method Parameters Description
PushReportData DataSet, Report object Pushes the DataSet into a Report object, and returns the report object
SetReportInfo ccReportInfo Header/Footer object, report object Pushes a ccReportInfo header/footer object into a report object
PreviewReport Report object, title string Launches a preview screen for a report object, and displays a preview screen title.
PreviewReport (Overload) Report object, title string, Dataset Combines PushReportData and the first overload of PreviewReport
PrintReport Report object Prints the report object, using print properties described in Table 2
ExportReport Report object, filename, ExportType object Exports a report object to the filename, as a format described in the ExportType object (valid options are PDF, Word, and Excel)
ExportReport (Overload) Report object, filename, ExportType object, start page, end page Performs same task as the first overload, but only for a specific page range

Table 2: Public properties for ccCrystalManager.
Properties Description
cPrinterName Name of printer to use
nCopies Number of copies
lAllPages Sets flag to print all pages
lPageRange Sets flag to print page range
nStartPage If printing page range, the starting page number
nEndPage If printing page range, the ending page number
lCollate Sets flag to collate printed copies
ExportTypes Public enumeration, items are PDF, MSWord, and MSExcel

Figure 11: Building a reusable print dialog box.
Listing 1 contains the source code for ccCrystalManager, which contains:

  • ccCrystalPrintOptionForm: a reusable print dialog box (see Figure 11).
  • ccCrystalViewer: a basic Windows Form with a Crystal Reports preview control.
  • ccReportInfo: a class to store report header/footer information that I covered in Tip 2. Table 3 lists the properties for this class.
Table 3: Public properties for ccReportInfo.
Properties Description
HdrCompany Company name, appears in the report header.
HdrReportTitle Report main title, appears in the report header.
HdrSubTitle1 Report subtitle, appears in the report header.
HdrSubTitle2 Second report subtitle, appears in the report header.
FtrDataSource Report data source, appears in the report footer.
FtrFootNotes Report user footnotes, appears in the report footer .
FtrRunBy User name of the user who ran the report, appears in report footer.
FtrVersion Version of software, appears in report footer.
UserID User key of the user running the report.

Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date