Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

The Baker's Dozen: 13 Productivity Tips for Crystal Reports and .NET : Page 3

These 13 tips show you how to build tools to address common reporting requirements for your business using Crystal Reports with .NET.


advertisement
Tip 2: Showing the Report Using the New Toolkit
Now that you've saved the report, you want to generate it from the application using the most recent product data. For demonstration purposes, you'll create sample data, rather than pulling the data from a back-end data source.

First, you need to add the report to your project as a typed report. The expanded development project in Figure 1 shows several Crystal Reports files in the project. By including reports in the project that will eventually be compiled into a DLL, you don't need to distribute the reports. You can reference a report and create a new instance of it just the way you would any other class, as follows:

RPT_ProductListing oReport = new RPT_ProductListing();

Second, you need to populate the typed DataSet definition with actual data. The sample project builds the necessary data for the report. So you need to push this data into the report and load the report.

Those new to Crystal Reports sometimes struggle with the exact syntax for pushing data into Crystal Reports, and even struggle a little with the process of displaying the report. Fortunately, the ccCrystalTools project provides several reusable functions to simplify these very tasks. Throughout the article I'll reference different functions that ccCrystalTools provides. In this case, the first function is perhaps the most powerful: after you populate the dataset, you can display the report on the screen with three lines of code:

RPT_ProductListing oReport = new RPT_ProductListing(); ccCrystalTools.ccCrystalManager oCrystal = new ccCrystalTools.ccCrystalManager(); oCrystal.RunReport(DsMyData, oReport, "Preview Title", "PREVIEW");

That's all there is to it! The function takes care of correctly pushing the dataset into the report. You'll see your report displayed in the standard Crystal Reports preview window (see Figure 6), along with the Crystal Reports toolbar to navigate through the report pages, print, export, etc. Note that some of the data is in bold and some in red: those are examples of formatting appearance at runtime through report formulas. I'll discuss them in Tip #7. I'll get into other options with the function a little later.

 
Figure 6: The Crystal Report Preview window provides lots of information.
Those who have written code to specify a report's datasource know that Crystal Reports is strict about receiving datatables in the same order they were defined at runtime. The RunReport function takes care of that—all you need to do is pass a dataset with the same table names that were used at report design time, and the function will take care of the rest. The RunReport function does so by using an internal method called SetData. This method scans through both the DataSet as well as the tables in the report's Database object, like so:



int nTableCtr = 0; // scan each table object in report, match it up // to the same datatable in the Dataset to be used foreach(Table oTable in oReport.Database.Tables) { foreach(DataTable Dt in DsReportData.Tables) if(Dt.TableName.ToString().ToUpper() ==oTable.Name.ToString().ToUpper()) oReport.Database.Tables [nTableCtr].SetDataSource(Dt); nTableCtr++; }

Later in this article, I'll discuss how you can use this same function with a report containing one or more subreports.

Tip 3: A Reusable Print Dialog
The previous tip shows how you can launch a Crystal Reports Preview form with minimal code. However, sometimes you may wish to print directly to the printer, without previewing. To print a report directly to the current Windows default printer, simply change the final parameter in RunReport() to PRINT or PRINTER (they both do the same thing). This parameter is particularly helpful if you're building a print process that doesn't require user prompts.

Of course, you may wish to set print options prior to printing. The Crystal Reports preview toolbar has a print option button that provides a print options tab. However, I've heard people ask how to provide the print options dialog box without needing to go through the preview screen.

The tool ccCrystalTools provides a Print Options form class (ccCrystalPrintOptionsForm) that your code can call just prior to printing a report:

ccCrystalTools.ccCrystalManager oCrystal = new ccCrystalTools.ccCrystalManager() ccCrystalTools.ccCrystalPrintOptionForm oPrintForm = new ccCrystalTools.ccCrystalPrintOptionForm(); oPrintForm.ShowDialog(); if(oPrintForm.DialogResult==DialogResult.OK) oCrystal.RunReport(DsMyData, oReport, "","PRINT");

The Print Options form contains many of the standard runtime print options, such as the printer to use, number of copies, page range, etc.

The print form ccCrystalPrintOptionsForm itself is fairly simple and allows users to select among the list of available printers and prompts to indicate number of copies, page range, etc. These selections are bound to properties in ccCrystalManager. Note the drop-down list of available printers: another common question I've heard is how to obtain a list of available printers. The System.Drawing.Printing namespace provides a PrinterSettings class including a collection of installed printers. You can scan through that collection to populate the printer selection drop-down list:

foreach(String cPrinter in PrinterSettings.InstalledPrinters) cboPrinters.Items.Add(cPrinter);

There's one more issue to discuss as part of this tip: Suppose you want to show the current Windows default printer as the default drop-down value, but it may not be the first one in the list. You can easily determine the current Windows default printer by starting a new Windows print document, and retrieving the PrinterName from the print document's printer settings:

PrintDocument prtdoc = new PrintDocument(); string strDefaultPrinter = prtdoc.PrinterSettings.PrinterName;

Once you've determined the name of the default printer, you can make that the default entry in the printer drop-down list selection. The Crystal Manager (ccCrystalManager) sets the Report object's printer name to the name of the selected printer, just before issuing the final call to print the report.

oReport.PrintOptions.PrinterName = this.cPrinterName; oReport.PrintToPrinter(this.nCopies, this.lCollate,this.nStartPage,this.nEndPage);

Note that the properties specified above are properties bound to the print options in ccCrystalPrintOptionsForm.



Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

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