03 557300 Ch03.qxd 3/24/04 9:40 AM Page 92
Chapter 3
When you click OK to exit the Chart Expert, your Chart will be added to your Report Header. When you
see the report in the designer, a pie chart should now be displayed in the Report Header. This isn’t an
accurate drawing of your graph. In fact it is nothing like your graph; it is just a placeholder to show
where in the report the graph will be positioned. If you preview the graph in a Windows form, it will
look something like Figure 3-34.
Another option to increase readability would be to make the chart larger. The chart object inserted into
your report is just like any other object in that it can be resized to fit your needs; you could even resize
the graph to take up the entire first page if that suits the needs of the report.
If you have a graph or chart inserted into your report, you can control the content by right-clicking
directly on top of the chart and selecting an option from the menu. The Chart Expert can be used to alter
the graph in the way that we have described so far in this section. You may have noticed that the Chart
Expert includes an option on the first tab labeled “Automatically set chart options.” If you uncheck this
box, two more tabs will appear, allowing you to control other aspects of the report, including the format-
ting for the graph axes and the general settings (like color, marker size, and so on).
Under Format Chart are the Template, General, and Titles options, which also allow you to customize
the appearance of your chart. The options in all of these menus are context sensitive; for example, you
can perform different types of formatting to a pie chart than you can to a bar chart. These options are all
relatively simple, so we’ll leave it to you to explore the almost endless possibilities.
Figure 3-34
92
03 557300 Ch03.qxd 3/24/04 9:40 AM Page 93
Designing Reports
Unfortunately, if you have used the retail version of Crystal Reports before, you will probably be won-
dering what happened to the Chart Analyzer, which allows you to open the graph in another tabbed
window. Crystal Reports .NET does not include the full capabilities of the Chart Analyzer, so if you
really need to use some of the advanced formatting features for charts and graphs, you are going to
have to buy a retail copy of Crystal Reports.
Working with Subreports
Within Crystal Reports .NET, multiple subreports can be combined into one main report, which allows
94
03 557300 Ch03.qxd 3/24/04 9:40 AM Page 95
Designing Reports
Figure 3-37
Second, if you want to add an existing report that has been saved outside of your project, use the Choose
a report option and the Browse button to locate the report you want to add as a subreport.
And finally, if you want to create a subreport from scratch, select the Create a subreport option, enter a
name for the subreport, and click the Report Expert button to launch the Standard Expert, which we
looked at in Chapter 2, to create your report. The Standard Expert is shown in Figure 3-38.
Figure 3-38
95
03 557300 Ch03.qxd 3/24/04 9:40 AM Page 96
Chapter 3
Once you have selected or created your report, click the Links tab to open the dialog shown in
Figure 3-39.
Figure 3-39
Using this dialog, you can create a linked subreport, where the subreport details will be filtered by a
field in your main report. To select a field from the main report, highlight the field and click the right-
arrow to add it to the list of selected fields. Then, using the drop-down lists at the bottom of the page,
select the corresponding field in your subreport.
Remember, you don’t actually have to specify any links at all, but if you do not, the subreport will not
be filtered — that is, it will run for all records in the subreport, wherever you place it. If you place an
unlinked subreport in the details section of your main report, for example, it will run once for every
detail record.
When you are finished, click OK to return to the report designer; your subreport will be attached to the
tip of your cursor, and you can click to place it on your report. It will appear as a box with a border
around it and a label for the subreport name.
Changing Subreports
To change the subreport name that appears when you preview your report, right-click the subreport and
select Format from the right-click menu. Using the option on the Subreport property page, you can
independently. This is a great way to ensure efficient reporting; the details contained within a subreport
are not returned until a user requests them.
To create an on-demand subreport, you can insert either a linked or unlinked subreport. Once the subre-
port has been inserted onto your report, you will need to set some options for on-demand reports.
Locate the subreport you want processed on demand, right-click directly on top of it, select Format, and
then select the Subreport tab, which will open the dialog shown in Figure 3-42.
There is only one setting required to process a subreport on demand, and that is the check box shown at
the top of the dialog. With this option enabled, a subreport will not be processed until the user clicks it.
The user may need a little prompting to understand what is going on. For this purpose, you can select
On-demand Subreport Caption by clicking the X+2 button. Using the Crystal Reports Formula Editor,
enter a caption for your on-demand subreport and enclose it in quotation marks. This text is what
the end user will see on the on-demand subreport link when previewing your main report. When you
are finished, click the Save and Close button in the upper left corner to exit the formula editor (see
Figure 3-43).
98
03 557300 Ch03.qxd 3/24/04 9:40 AM Page 99
Designing Reports
Figure 3-42
Figure 3-43
99
03 557300 Ch03.qxd 3/24/04 9:40 AM Page 100
Chapter 3
Saving and Reimporting Subreports
Subreports can be saved to a separate, independent report file and can also be reimported when the
main report is opened. Saving subreports to an external file allows you to break up main reports and
subreports so users can print the subreport independently or use it in other reports. To save a subreport,
right-click directly on top of the subreport object in your main report and select Save Subreport As.
Specify a file name and click OK, and your subreport will be saved to a separate file.
Reimporting subreports provides a creative way of using them. Using this facility, you can create a num-
ber of reports that can serve both as subreports and as reports in their own right. This setting is available
what data is brought back from the database.
Creating a Parameter Field
The first step in creating a parameter field is specifying a name for that field. Once you have created
your parameter field, Crystal Reports will enclose this field name in curly brackets, preceded by a ques-
tion mark to indicate it is a parameter field (
{?ParamFieldName}). You can also enter prompting text
that will appear whenever the Parameter Field dialog appears, as shown in Figure 3-45. Prompting text
should help the user understand what to enter in the value field of the dialog (for example, “Please enter
a state for this report.”).
By default, any parameter you create will have a default type of String, but there are actually seven dif-
ferent field types you can use, including:
String For entering alphanumeric text
Currency For prompting users to enter an integer with two decimal places
Date Used to enter a standard date, in the format Month/Day/Year
Date Time For prompting for a date/time string, in the format
Month/Day/Year Hour/Minute/Second/AM
Time For entering the time in the format Hour/Minute/Second/AM
Number Used to enter a number with variable decimal places
Boolean Used to prompt users for a true or false response
101
03 557300 Ch03.qxd 3/24/04 9:40 AM Page 102
Chapter 3
Figure 3-45
Which field type you choose depends on how you are going to use the field in your report. Other
attributes that can be set when creating a parameter field include:
❑ Allow Multiple values — Allows you to enter a list of values for your parameter field
❑ Discrete — Allows you to enter a single value
❑ Range — Allows you to specify an inclusive range, using a start and value
❑ Discrete and Range — Allows a combination of the previous two attributes
Most of the attributes discussed here are optional; to create a parameter field, the only requirement is
have improved performance and cut down on processing time, but the majority of poor report perfor-
mance does not lie within the Report Designer but rather in how the report is designed and in the
underlying data.
For example, if you have a report that has been developed across an Oracle table that contains 500,000
rows of data, the report is going to take a while to run, regardless of whether you are using Crystal
Reports .NET or just submitting an SQL Statement from a PL/SQL command prompt. If you believe that
a report’s performance could be improved, ask your DBA or architect to review the tables, views, and
other data impedimenta that you are using in order to verify that you have used the correct primary and
foreign keys and that you have taken the most direct route to join tables in your report.
104
03 557300 Ch03.qxd 3/24/04 9:40 AM Page 105
Designing Reports
Following from that, you also may want to take the SQL that Crystal Reports .NET generates (right-click
your report and select Database → Show SQL Query) and paste it into SQL*Plus Query Analyzer or any
other SQL query tool supported by your database platform to see how long it runs. Your DBA could also
provide suggestions on ways to improve the SQL statement generated, and you can then use the opti-
mized SQL as the basis for your report.
With Crystal Reports .NET itself, there are a couple of options that can help with performance. To view
these options, right-click your report and select Report → Report Options.
There are two options in this dialog that can aid with performance: The first, Use Indexes or Server For
Speed, for use with databases that use indexes, will use the index on the database server to sort and
retrieve records faster than if the index was not used. The second option, Perform Grouping On Server,
for reports that have groups, will push the grouping back to the database server, provided that the
details within your report are suppressed. (We can’t show detailed records in the report because it
actually changes the SQL statement and uses a GROUP BY clause.)
Summary
This chapter ran through the laundry list of Crystal Reports features and functionality without even
stopping to take a breath! Most report designers will not use all of these features in a single report, but
hopefully this chapter will serve as a reference that you can turn to as you need to add different features
to your own reports.
Figure 4-1
As you go through the chapter, you will be able to actually look at the application we are creating and
other examples that illustrate points along the way.
Planning Your Application
Integrating reports is an easy way to add value to your application and can be an important component
of your application’s offering. Before we get started on actually integrating Crystal Reports .NET with
your Windows application, we need to do a little planning to make sure the integration goes smoothly.
First, we need to have a report (or suite of reports) to work with. In Chapter 2, “Getting Started with
Crystal Reports .NET,” we walked through the report planning and design process, so you should have
a start on the skills you need to design reports.
108
04 557300 Ch04.qxd 3/24/04 9:36 AM Page 109
Report Integration for Windows-Based Applications
If you flipped straight to this chapter or haven’t gotten into designing your own reports yet, there are
some sample reports located in
C:\program files\Visual Studio .Net 2003\Crystal Reports\
Samples\
that you can use to practice concepts from this chapter; alternatively, there are the sample
reports included in the download file as well.
We need to plan for how those reports will be delivered to users and the forms that will be required to
host them. Crystal Reports .NET uses a feature-rich report viewer that can be inserted onto a Windows
Form and used to view reports. The viewer itself has an extensive object model, allowing you to set the
source of the report, the appearance of the viewer itself, and what happens when different events fire.
Most applications can utilize a single Windows Form hosting the Crystal Report Viewer and simply pass
properties like the report source and viewer settings to this form. This lends itself to a number of cre-
ative solutions for user personalization and settings. You could store viewer settings and preferences in a
table or XML file for each user (or group or role) and apply these settings when viewing a report.
In addition, you could also set specific record selection formulas for different groups of users, allowing
them access only to the data applicable to them. You could also create a custom user interface, allowing
users to set and retain parameter settings for future use, or even to keep their printing or export prefer-
correspond to the sections in this chapter. If you want to get down and get your hands dirty creating
your own project as we go along, then from within Visual Studio, select File → New → Project, and from
within Visual Basic Projects, select Windows Applications and specify a name, as shown in Figure 4-4
(in the sample code, we have called this project
viewer_basic and we have saved it to
C:\Crystal.NET2003\Chapter04)
110
04 557300 Ch04.qxd 3/24/04 9:36 AM Page 111
Report Integration for Windows-Based Applications
Figure 4-3
Figure 4-4
111
04 557300 Ch04.qxd 3/24/04 9:36 AM Page 112
Chapter 4
Throughout the chapter, we will be using only one or two forms to demonstrate different integration
features; your own applications will probably have multiple forms and launch reports from any number
of them, but the same concepts can be applied.
Before we actually look at any code, we need to go back to the integration features that you want to
incorporate into your application and select the appropriate object model(s).
Determining the Correct Object Model
When working with Windows applications, you have two different object models to choose from,
depending on your particular needs. The first, contained within the Crystal Reports Windows Forms
Viewer object model (
CrystalDecisions.Windows.Forms), contains all of the functionality required to
view a report in the Crystal Reports Windows Forms Viewer. It also includes the ability to set database
logon information, pass parameters and record selection, control the viewer’s appearance, and view
reports, including reports consumed from an XML Report Web Service.
Using this object model, you can satisfy most basic report integration requirements, but you have no
control over the report itself; you won’t be able to change the record selection for any subreports that
appear in your report, and you won’t have access to modify report elements, such as groups, and sorting
back to some function within the viewer itself. In the section immediately following, we are going to
look at each of these classes in depth and learn what can be done with each.
Class Description
CrystalReportViewer Contains the properties, methods,
and events relating to the
Crystal
ReportViewer
and viewing reports.
DrillEventArgs and DrillSubreportEventArgs Provides data for the Drill and
DrillDownSubreport events on main
and subreports.
Drill events fire when a
user drills down into a group or summary
on a particular report or subreport.
ExceptionEventArgs Provides data for the HandleException
event. A HandleException event occurs
when there is an error or exception when
setting the report properties or viewing
the report. It is primarily used for trou-
bleshooting and error messages.
NavigateEventArgs Provides data for the Navigate event.
When a user navigates through the pages
of a report, the
Navigate event fires each
time. This can be used to notify the users
when they have reached the last page, call
custom actions, and so on.
SearchEventArgs Provides data for the Search event. The
CrystalReportViewer includes an inte-
grated search function to search for values
In this example, we are going to use the
viewer_basic project, which you created earlier. In the project,
there should be a default
Form1 that we can use. Locate the CrystalReportViewer within the toolbox
and drag or draw the Report Viewer onto your form.
Once we have added the Report Viewer to our form, we now need to set the
ReportSource property
to point to the existing report file. The sample files for this chapter include a sample report
(
ch4_worldsales.rpt) that we can use. Using the property pages for the Crystal Report Viewer, set
the
Report Source property to the location of this report file. For instance, if you have put the sample
files in your own personal directory, enter the full report file name and path,
(
C:\CrystalReports\Chapter04\ch4_worldsales.rpt).
You can now run your application, and when you preview the form, your report will be displayed in the
Crystal Reports Viewer, as shown in Figure 4-5.
The viewer interacts with the Crystal Reports Print Engine, runs the report, and displays the results.
From your report preview, you can drill-down into the details, search for a value, print, and export (as
shown in Figure 4-6) without having to do any additional coding.
If you only have one or two reports that you want to integrate into your application and you don’t need
to customize any features at run time, this may be all you need. However, for applications that require a
more sophisticated integration with Crystal Reports .NET, you probably need to look a bit further.
114
04 557300 Ch04.qxd 3/24/04 9:36 AM Page 115
Report Integration for Windows-Based Applications
Figure 4-5
Figure 4-6
115
04 557300 Ch04.qxd 3/24/04 9:36 AM Page 116