0% found this document useful (0 votes)
54 views32 pages

CrystalReports Capitulo02 AdicionarReporte

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
54 views32 pages

CrystalReports Capitulo02 AdicionarReporte

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 32

CR.

NET Book by Bischof- Chapter 2 - Copyright 2003 Page 1 of 32

Since Crystal Reports is fully integrated with Visual Studio .NET, you may be tempted to think that
creating a new report is a simple matter that only takes a minute or two. While this is true after you have
a little experience, the first time you create a report can be confusing if you don't follow the proper steps,
and in the right order. This chapter shows you what steps are required and how to do them. This
provides the foundation for all the chapters that come after it. After a writing a couple of reports, you
will find that these steps are second nature and it will be easy to quickly create new reports. The steps
are broken down into two categories: creating a report and printing/previewing a report.

Reports are files that must be created with the Visual Studio IDE. This requires opening a project
and building the files within it. Although the reports are part of this project, they are saved as files
independent of the project itself. Thus, you can open and print the reports from other .NET projects.
This gives you the flexibility to create a project that is a library of reports that are used by a multitude of
projects. These projects can be Windows based or called from an ASP.NET application. This section
shows you how to create a report file. You can decide whether to use this report in the same project that
it was designed in or to call it from an outside application such as another Windows or Internet
application.
Adding a report to a project involves 5 steps. Each step has different options that you need to
consider before implementing it. A summary of the steps for adding reports to projects are listed in
Table 2-1.
Table 2-1. Steps for writing a report.
Step Description
1. Creating a new report file From the menu, select Project/Add New Item. Follow
the prompts to select the report type.
2. Running the report experts If you don't want to start with a blank report, then you
can use the different report experts to identify the
tables and fields to print and get a good start in the
right direction. This is optional.
3. Setting the designer's defaults Make sure your development environment is the way
you want it before working on your report.
4. Adding report objects to the report Add the different objects that your report needs. These
objects consist of textboxes, fields, etc.
5. Formatting the objects Set the properties of the objects so that they are
formatted properly. These properties consists of fonts,
sizes, etc.
Before you add a report to your project, you need to know what your reporting goals are and how the
report will help fulfill these goals for your application. None of the options presented here are complex.
In fact, they are quite simple. You just need to be aware of what they are in advance. The following
sections explain each step in detail and tell you what you need to consider when doing each one. The
last section of the chapter, Coding Samples, pulls together everything you learned in this chapter and
shows sample code of how to print and preview reports.

Creating a New Report Class


To create and print a report, you need to create a new report class for your project. Once the class is

http://www.WillyDev.Net
CR.NET Book by Bischof- Chapter 2 - Copyright 2003 Page 2 of 32

created, you can modify it using the report designer and then call it from your application.
Creating a new report class is done two different ways. The first is to select the menu options
Project/Add New Component. The other way is to right-click on your project name in the Solutions
Explorer window and select Add/Add New Component. Both of these methods open the Add New Item
dialog. Scroll down near the bottom to select Crystal Report, type in a name, and click Open
The Crystal Report Gallery dialog box opens (see Figure 2-1). You are given the option of using the
report expert, creating a blank report, or creating a report using an existing report. If you choose the
Report expert option, you can tell it the type of report expert to use in the list box below the options. If
you choose to start with a blank report, the gallery goes away and a new report is created. This report
gives you the five basic report sections and each is empty. Add report objects to the proper sections and
format them to build your report. If you choose to create a report from an existing report, you are given
an Open File dialog box where you find and select the report to use as a starting point. This report file is
copied into your local directory. All changes are made to the local report and do not effect the original
report.
You can also create a new report based off an existing report by selecting Project | Add Existing
Item. This skips the Gallery dialog box and immediately shows you the Open File dialog box. The report
files are saved with a .RPT extension.

Figure 2-1. The Report Gallery dialog box.

Setting the Designer's Defaults


The report designer is where you will spend all your time creating reports and modifying them. There
are many different default settings that you can set that control how you interact with the report designer

http://www.WillyDev.Net
CR.NET Book by Bischof- Chapter 2 - Copyright 2003 Page 3 of 32

as well as controlling how you access and display data on a report. Knowing what the different options
are will not only make youmore efficient, but it can also save you a lot of headaches. It is very likely
that once you set these default values you will not come back to this step anymore. However, when
starting a new application, it would be beneficial to set the default values for how different report objects
are displayed. For example, if you decide to always make group headers a certain font, then you can set
that to be the default and you won't have to modify it every time.
To modify the default settings, right-click anywhere on the report and select Designer/Default
Settings. This makes the Default Settings dialog box appear. It is shown in Figure 2-2. There are seven
different tabs that control the default settings. Each tab effects different parts of the designer and the
report output.

Figure 2-2. The Default Settings dialog box.

The Layout tab effects your interaction with the report designer. The Field Options frame sets how fields
are shown on the designer. You have the option of showing the names of the fields that are being
displayed, the names that were assigned to each object, or format symbols. Showing the format symbols
is useful for seeing how the different numbers will be formatted and seeing the maximum width that a
string can use.
The Grid Options frame will be discussed in the Adding, Resizing and Moving Controls section. It
lets you turn the grid markers on and off and set whether objects must align with them. The View
Options frame sets what is shown in design mode.

http://www.WillyDev.Net
CR.NET Book by Bischof- Chapter 2 - Copyright 2003 Page 4 of 32

The Database tab effects how items are displayed in the Field Explorer and it has settings for tweaking
performance. It is shown in Figure 2-3. The settings for changing the Field Explorer are many. They
consist of deciding what types of objects are to be displayed and how the items will be sorted.

Figure 2-3. The Database tab.


Performance related settings are listed in the Advanced Options frame. The setting to use indexes on
the server is set by default. Using indexes on the server gives you better performance because the server
is optimized for doing this. You may decide to turn this off if the server is being overloaded with report
processing and the reports are being run in a batch format where speed isn't as much of a factor. The
other performance related setting is to perform grouping on the server. Just like performing indexes on
the server is faster, so is having the server do the grouping. Unfortunately, you can only use this option
for accessing SQL tables directly. It isn't available when reporting off queries.

This tab modifies that fonts that the Formula Editor and SQL Expression Editor uses to display the
programming code. You are free to customize the different programming elements to your heart's
content.

This tab lets you specify a default folder for where the database files are located. This is used with the

http://www.WillyDev.Net
CR.NET Book by Bischof- Chapter 2 - Copyright 2003 Page 5 of 32

Data tab of the Report Expert. When selecting Database Files it will display the Open dialog box and
default to the folder you specified.

The Reporting tab effects the output of your reports. It is shown in Figure 2-4. The top frame lets you set
how data is read from the data source. You can automatically convert DateTime data to a String, a Date,
or keep it as a DateTime data type. See Chapter 8 for a discussion of the date data types. Within this
frame you can also convert NULL fields to their default value. This is very useful for ensuring that
numeric fields are printed as a zero and not an empty field.

Figure 2-4. The Reporting tab.


The bottom frame has miscellaneous options in it. You can tell the report to discard saved data when
loading reports. Reports can save their data so that they don't have to reload and process the records
every time (this really improves printing speed). If you want your reports to discard the old data and
always re-query the data source for the latest data, then turn this option on. You can also tell it to re-
import subreports so that they are always refreshed when printing a report.
You can set whether drill-down reports will show the column names for the drill-down data. A
preview picture (thumbnail) can be saved every time you run a report. The last option lets you set the
default formula language as discussed in Chapter 7.

Every data type that can be displayed on a report has a default format that it gets displayed in. For
example, the default for displaying a number is to use two decimal places. The Fields tab lets you set the
default formats for all the available data types (see Chapter 8 for a list of data types). By changing the

http://www.WillyDev.Net
CR.NET Book by Bischof- Chapter 2 - Copyright 2003 Page 6 of 32

default here, every new object that is added to your report after changing the default will use this setting.
Any objects that were added before you made the change will not reflect the new format.

Just as the Fields tab sets the default format for the different data types, the Font tab sets the default font
for the different fields on a report. For example, you can make field titles appear in italics while group
names appear in bold. Once again, these changes will only effect objects that are added after you make
your changes.

Using the Report Experts


Depending upon you needs, creating reports can be a simple or complicated process. It all depends upon
your needs. For example, it is very easy to print mailing labels from an address database or send out a
form letter to a list of subscribers. On the other hand, it can be very complicated to write a report that
uses multiple sub-reports which are based off of user entered parameters. Fortunately, the report experts
that come with Crystal Reports, often referred to as "Wizards" in other applications, make it easy to
quickly produce many reports. With just a few clicks of a button you can create a report that pulls fields
from a database, organizes them and even prints a color chart! In fact, the experts are useful for writing
complex reports because they give you a way to quickly make a professional looking report template
that you can then customize for your specific needs.
The purpose of the experts is to make it easy for someone to answer a series of questions about a
topic. The computer takes the answers to the questions and performs a series of tasks to create the
desired result. Crystal Reports uses experts to create seven types of reports: Standard, Form Letter,
Form, Cross-tab, Sub-report, Mailing Label, and Drill-Down. Each of these reports has a separate expert
that guides you through the process of building the report. Even though each expert is different, they are
very similar because they share the same tabbed dialogue boxes. The difference lies in the fact that each
one uses a different combination of these dialogue boxes.
Since experts are designed to be easy to use, you might be wondering why this chapter needs to
explain them. After all, something that is simple to use really shouldn't need any explaining. While this
was probably true for the first generation of applications that used experts, today's applications have
added many bells and whistles to their experts to make them more functional. The problem with experts
is that they are supposed to be simple to use, but anything that is simple is generally not very useful. As
a result, experts in today's applications are designed so that not only can you quickly answer the
questions presented and create the final product, but you can also click on various buttons and
checkboxes to add advanced functionality. This gives you the best of both worlds: a simple to use
interface with extra features for advanced users.
The report experts give you the ability to quickly create a report as simple as a form letter or
something as complicated as the dreaded cross-tab report. Within each expert are various options you
can select to add functionality to your report. Letting the expert add the functionality means that you
don't have to.
Each expert consists of a multi-tabbed dialog box. Many of the tabs on each expert are used
elsewhere within the main functionality of Crystal Reports. This book covers each tab within the chapter
that applies to it. For example, the Data tab is summarized here, but it is fully explained in Chapter 14.
Rather than repeat this information here, this chapter summarizes each how to use each tab and it makes
a note of any differences between it and the main tab used in the program. Table 2-2 shows which tabs
are used by the different report experts.
Table 2-2. The report experts summarized.
Tab Standard Form Form Cross- Sub- Mailing Drill-

http://www.WillyDev.Net
CR.NET Book by Bischof- Chapter 2 - Copyright 2003 Page 7 of 32

Letter Tab Report Label Down


Data X X X X X X
Links X X X X X
Fields X X X X X
Group X X X X X
Total X X X X
Top N X X
Chart X X X
Select X X X X X
Style X X X X
Customize X
Style
Form Letter X
Form X
Cross-Tab X
Drill X
The Form Letter expert has two places where you can add fields to your report. The first is the Fields
tab. As described later, this is where you pick which fields to display on the report. The expert adds
these fields to the Details section. When you get to the last tab, the Form Letter tab, you have the option
of filling in text as well as fields into each section. This is saved to your report as a large textbox in each
section. In the details section, this textbox will be placed above the fields you specified in the Fields tab.
There are two primary differences between the Form Letter expert and the Form expert. The first is
that the Form Letter expert gives you a Total tab for performing summary calculations on fields and the
Form expert doesn't. The other difference is that although both the Form Letter expert and the Form
expert have a tab that lets you add information to each section of the report, the Form Letter tab only lets
you add text and the Form expert only lets you add images.
Since the Form expert lets you add images to each section of your report, this makes it easy to create
reports that are printed on standard forms such as invoices or statements. Just scan in the form and add it
the proper section. Then put your fields on top of the image at the exact location. When you are done
adding all the fields you can delete the image and everything will line up perfectly.
The Subreport expert makes it easy to add a subreport to an existing report. It provides the same tabs
as the Standard expert.
The Mailing Label expert is used to create mailing labels. The biggest stumbling block to writing a
mailing label report is to get each item to line up properly with the actual labels. The expert makes this
easy by giving you all the standard Avery label numbers. If needed, you can also customize the label
coordinates for unique layouts.
The Drill Down expert is used for creating reports that only expose a portion of the data at any given
time. It starts by showing a summary of the data. The user clicks on the field that they want to see more
information on and the report exposes the details. This is discussed in Chapter 12.

Optimizing with the Tabbed Dialog Boxes


Each report expert uses a combination of different tabbed dialog boxes to question you about how to
build your report. Each expert presents a slightly different combination of these tabs. This section
describes how to use each tab and explains any aspects of it that may not be obvious. The report expert
tabs are called Data, Links, Fields, Group, Total, TopN, Chart, and Select.

The Data tab is the first dialog box presented after you select which type of report you want. It lets you
select the database and tables that store your data. The database can be a standard data source such as

http://www.WillyDev.Net
CR.NET Book by Bischof- Chapter 2 - Copyright 2003 Page 8 of 32

SQL Server or a non-standard data source such as an Excel spreadsheet. Figure 2-5 shows an example of
using the Data tab to select a data source that is an ADO.NET DataSet called ReportExperts.DataSet1.

Figure 2-5. The Data tab in the Report Expert.


The window on the left lists all the categories of data sources. Select the one you need to connect to
your data source. This Data tab is the same data tab that is explained in detail in Chapter 14. See that
chapter for more information.
An oddity that occurs on this tab is that once you add a data source to the available data sources
window, you can't remove it. Neither expert has any menu options or buttons to do so. Fortunately, this
won't create any problems. When you close and save your application, Crystal sees that you added a data
source that isn't being used by the report and it automatically remove it for you. You can think of the
window on the left as being used to specify which data sources are available to the report. When you are
finished creating the report, Crystal Reports realizes that one or more data sources wasn't used so it
removes it from the list. If you do need that data source in the future, you can always add it back again
with the Database expert.
Add the tables you need to the window on the right. There is one small difference between this Data
tab and the Database Expert described in Chapter 14. Once you add a table to the Selected Tables
window, you can't delete it from here. There are no menu options available to remove it from the
window. To delete a table from this window you have to exit the report expert and open the Database
Expert dialog box by right-clicking on the report and selecting Add/Remove Tables.

Reports that use two or more tables need to have these tables linked so that data can be pulled from both

http://www.WillyDev.Net
CR.NET Book by Bischof- Chapter 2 - Copyright 2003 Page 9 of 32

of them. The Links tab lets you set the fields for creating relationships between the tables. Crystal
Reports automatically attempts to link the tables together by using common field names. If two tables
each have a field with the same name and the data types are compatible, then Crystal will link them
using this field. Figure 2-6 shows an example of the Links tab with two tables linked together.

! "#$ %
These automatic links are not set in stone and you are free to delete them or add more as needed. To
delete a link, click on it and press the delete key or click on the button labeled Delete Link. To add a
new link, drag and drop the field from one table onto the matching field in the other table.
The Links tab is identical to the Links tab in the Database Expert discussed in Chapter 14. See that
chapter for more information.

After selecting which tables you want to use for your report, you need to select which fields will be
shown on it. The Fields tab is where you do this. Figure 2-7 shows an example of what it looks like. The
window on the left, titled Available Fields, shows the fields that you can choose from. The window on
the right, titled Fields To Display, shows the fields that have been selected. Adding and deleting fields is
done in the standard manner of dragging and dropping them between windows or selecting a field and
clicking on the appropriate arrow button. You can also add a field by double-clicking it in the left
window. However, you can't remove a field by double-clicking on it in the right window. After a field
has been added to the report, you can change the default column heading for it by modifying the text
box below the window.

http://www.WillyDev.Net
CR.NET Book by Bischof- Chapter 2 - Copyright 2003 Page 10 of 32

Figure 2-7. The Fields tab of the report experts.


There is a Formula button that you can use to add a formula field to your report. As discussed in
Chapter 7, a formula field is a way to create new fields by using existing data. After you create a
formula and save it, it will appear in the left window under the Report Fields category. You can select it
from here and add it to your report. See Chapter 7 for more details on using the Formula Editor.
A very informative button is the Browse Data button. This comes in handy when you aren't sure
whether a particular field is needed on a report and you want to see some example data for it. By
selecting the field you need to view and clicking the Browse Data button, a window appears listing the
data that is in the database for that field. This lets you immediately see what type of data the field stores
without having to open another program. Quite convenient!
Another useful button is the Find Field button. If you know that a certain field should exist in one of
the tables you selected, but don't know which table its in, then click on the Find Field button and enter
the field name. It will find the field and display it in the window.
After you add all the necessary fields for you report, you are free to reorder them by using the arrow
buttons above the window. Select the field to move and click the up or down arrow to reposition it.

Some reports have so much data on them that they can be a little hard to understand. When reports are
dozens of pages and even hundreds of page long, you need to organize the information in a way that
makes it easier to absorb the data in smaller pieces. You do this by creating groups within the report.
Groups can be based on many things. Some examples are grouping on months of the year or the names
of the different branch offices for a company.

http://www.WillyDev.Net
CR.NET Book by Bischof- Chapter 2 - Copyright 2003 Page 11 of 32

The fields that you group on do not have to be the fields that you selected to be in the report. Figure
2-8 shows an example of the Groups tab and you can see that all the fields in the database are available
for selecting. Thus, the fields that you've selected to be in the report on the previous tabs don't limit you

Figure 2-8. The Groups tab of the Report Expert.


The Group tab is pretty simple. The window on the left, titled Available Fields, shows all the fields
available for grouping. You can select any of these to be added to the window on the right, titled
Grouping Fields. The order that you arrange them in this window is important because the top-most
group will be sorted first and the resulting data within that group will be sorted based on the next
corresponding group field. If you want to rearrange the grouping order, you can use the arrow keys
above the right window to do so.

& ' ( )
*

Each grouping field can be sorted in a variety of ways. Once you add a field to the grouping
window, a drop-down box appears that lets you select the sorting order. The possible sorting orders are
ascending, descending, original order, and specified order. Choosing original order prevents the groups
from being sorted at all. Thus, the groups are displayed as they appear within the raw data.
Grouping records in a specified order is discussed in detail in Chapter 12. The functionality is the
same as here, but the buttons are laid out differently. In this case, the drop-down box directly below the
window is where you choose an individual value. Adding a new sub-group is done by clicking the New

http://www.WillyDev.Net
CR.NET Book by Bischof- Chapter 2 - Copyright 2003 Page 12 of 32

Named Group button. Modifying the definition of a sub-group is done by clicking on the Edit
Named Group button. The Others button defines how to handle any values that didn't get defined within
the values or sub-groups. You can either leave them out of the report, put them in another group that
appears at the end of the report, or let them stay individually in the report.
The lower list box is where the different values and sub-groups are listed. They are listed in the order
that they will appear in the report. If you add the items in the wrong order, you can't change that order in
the Report Expert. You have to finish using the Report Expert then right-click on the report in design
mode and select Report/Change Group Expert.

It is very common for reports to calculate sub-totals and other summary calculations on the numeric
fields. The Total tab, shown in Figure 2-9, is where you define the summary calculations for the
different fields. The left listbox shows all the available fields. The right listbox shows the fields that will
have summary functions calculated for them. By default, the expert automatically populates this list with
all the numeric fields that are in the report. Just like the other tabs, you can add and remove fields from
this list by either dragging and dropping them or by using the Add and Remove buttons.
Below the Summarized Fields list is where you select the type of summary calculation to perform on
the field. These calculations are explained in more detail in Chapter 12. There is also a checkbox that
tells the expert to automatically add a grand total field as well.

Figure 2-9. The Total tab of the Report Expert.

http://www.WillyDev.Net
CR.NET Book by Bischof- Chapter 2 - Copyright 2003 Page 13 of 32

When adding a group to a report, you probably assume that every record within the group will be
displayed. In most circumstances this is the case. However, you can tell Crystal Reports to only display
a certain number of records based upon their rank. For example, you could have a top salesperson report
where you show the top 10 salespeople in your office. You could also have another report that shows the
worst 10 salespeople. The Top N tab lets you do this by sorting the groups based on a summary field.
This tab is shown in Figure 2-10. See Chapter 12 for more information because it is identical to the Top
N Expert shown there.

Figure 2-10. The Top N tab from the Report Expert.

This tab lets you add a chart to your report. You can also set a variety of options for what type of chart
to display and how to display it. This tab is shown in Figure 2-11 and it is identical to the Chart Expert
discussed in Chapter 11.

http://www.WillyDev.Net
CR.NET Book by Bischof- Chapter 2 - Copyright 2003 Page 14 of 32

Figure 2-11. The Chart tab from the Report Expert.

Some reports need to only show a sub-set of all the data that is in a recordset. For example, a financial
report may only show the corporate data for a single quarter or a range of quarters. To filter out certain
data so that you limit how much information is shown on a report, use the Select tab shown in Figure 2-
12. This tab lists all the fields you can choose from and you can specify criteria that are used to include
or exclude certain records. This tab is the same as the dialog box for the Select Expert discussed in
Chapter 12.

http://www.WillyDev.Net
CR.NET Book by Bischof- Chapter 2 - Copyright 2003 Page 15 of 32

Figure 2-12. The Select tab from the Report Expert.

Once you are finished specifying the fields to appear on your report and all the minute details of
selecting the proper records, you can use the Style tab to format everything so that the report has a
professional look to it. This tab, shown in Figure 2-13, lists 10 different pre-determined formats that can
be applied to your report. These styles make it easy for you to take some raw data and spice it up enough
to make everyone think you worked really hard on this report! Some of the styles you can choose from
are Standard, Drop Table, and even the colorful Maroon/Teal Box style. Another benefit to the Style tab
is that you can use the same style for all the reports in an application and this gives them a consistent
look and feel which makes your application more professional.

http://www.WillyDev.Net
CR.NET Book by Bischof- Chapter 2 - Copyright 2003 Page 16 of 32

Figure 2-13. The Style tab from the Report Expert..

This tab is only available when using the Form Letter Expert. It lets you write the entire form letter
within this tab. The tab is shown in Figure 2-14. At the top is a drop-down box that lists the different
sections of the report. For example, it lists the Report Header, Page Header, any group header sections
and the Detail section. Select the section you want to write and then in the scrollable textbox below it,
type the text. You can also insert formulas or fields from the data sources in this textbox and the expert
keeps track of it all. When you are finished and you close the expert, it adds a large text box to your
report for each section and fills in the information you entered.

http://www.WillyDev.Net
CR.NET Book by Bischof- Chapter 2 - Copyright 2003 Page 17 of 32

Figure 2-14. The Form Letter tab from the Report Expert..

This tab, shown in Figure 2-15, is only available when using the Form Expert. It gives you a way to
easily add a picture or logo to the sections of a report. Notice that when you add a picture using the
expert, it might not be sized properly and could look distorted. Don't worry about that because it will
look fine on the report and if you need to you can always change its properties in design mode.
It also makes it easy to write reports that print onto standardized forms. Scan in the form that you
want to print onto. Then use an image editor to crop it into the appropriate sections: Header, Detail,
Footer, etc. Insert each section into the proper section of your report. Then add the fields so that they
line up exactly with the scanned image. When you are finished adding all the fields, delete the images.
This leaves you with only the fields to print and they are lined up perfectly.

http://www.WillyDev.Net
CR.NET Book by Bischof- Chapter 2 - Copyright 2003 Page 18 of 32

Figure 2-15. The Form tab from the Report Expert..

!
These tabs is only available when using the Cross-Tab Expert. They let you modify the cross-tab data
for what appears in the rows and columns. Modifying these tabs is very involved and is explained in
detail in Chapter 10.

The Drill tab is only available when using the Drill-Down Expert. When creating drill-down reports, you
allow the user to see a summarized view of their data. If they want to examine the data in more detail,
they can expand a record to see more detail relating to the summarized information. The summarized
fields are listed on this tab. When you click on the field's name, it will toggle it back and forth between
"Show" and "Hide". This will alternate the default of whether it gets displayed or not.

Using the Report Objects


A report is very similar to a Windows form. Just like a form, the report is listed as a separate item under
your project in the Solution Explorer window, there are various controls that can be added to it, and it is
a class that has to be instantiated before using it. The objects that are added to a report are similar to the
ones you use when building forms. This section describes the different objects and how they are used. It
also gives you a reference for the properties of each object. Every single property isn't listed because
that would require reprinting the MSDN documentation. But the properties you'll need to use on a
regular basis are highlighted here.

http://www.WillyDev.Net
CR.NET Book by Bischof- Chapter 2 - Copyright 2003 Page 19 of 32

There are three controls in the report toolbox (see Figure 2-16). You can access those same controls
in addition to many more by right-clicking on the report and selecting the Insert menu option (see Figure
2-17).

Figure 2-16. The control Toolbox.

Figure 2-17. The Insert menu option.

http://www.WillyDev.Net
CR.NET Book by Bischof- Chapter 2 - Copyright 2003 Page 20 of 32

" # ! $ % & '


The objects on a report work the same as the controls on a form. The objects that are listed in the
toolbox can be added to the report by dragging and dropping them onto the proper section of the report.
You can also double-click on them and they will be automatically added to the section that has the focus
(its header bar will be blue while the other header bars will be gray). If you have an existing control that
you want to reuse and it has already been formatted, then you can highlight it and copy and paste it. This
creates a copy that is attached to your pointer and will move around as you move your mouse. When
you have it positioned properly, click the mouse button to drop it there. You can also select multiple
objects for copy and paste.
Selecting multiple objects is done by holding down the control or shift key and clicking on the
individual controls. You can also draw a temporary box on the report and any controls that are included
in the box get selected. Do this by holding down the mouse button and moving the mouse to enlarge the
box. Let go of the mouse when the box is complete and the objects will be selected.
There is a strange behavior to be aware of when selecting objects using the box technique. You can't
draw a box if another object is already selected. You have to first click anywhere on the report to
unselect the current object and then you can draw the box. For example, assume that you selected a
textbox object. Then you decide that you really wanted to select multiple textboxes so you click
elsewhere on the report and attempt to draw a box. Unfortunately, nothing will happen. It only results in
the textbox getting unselected. You need to click the mouse again to start drawing the box.
You can only select multiple objects when they are compatible. For example, the box and line
objects can be selected together, but the box object can't be selected with the text object.
Resizing a control is done by selecting it to give it the focus. Then position the mouse over the sizing
handles on any side and drag them. When resizing multiple objects, the sizing handle will only appear
on the last control selected. As you resize the last control, its new size changes as you move your mouse.
The other controls will not change until you release the mouse button. An option for resizing objects is
to let Crystal adjust their size to be the same for each one. After selecting all the objects, right-click on
one and select the Size menu option. From there you can choose Same Width, Same Height, or Same
Size.
When moving objects on the report, it can be helpful to display the grid lines. This makes it easier to
line up objects with each other. You have the option of making the objects snap to the grid lines. This
means that when you move a control, its edge must be placed on a grid line. It can't be placed between
grid lines.
When you release the mouse, the object will automatically snap to the nearest grid line. Turning grid
lines and the snap-to feature on and off is controlled by changing the designer properties. Right click on
the report and select Designer/Default Settings.
Turning on the snap-to grid lines option can be a blessing or a curse. There are two problems that
can occur when this option is turned on, and they both seem to occur most frequently with reports that
have been created with the report experts. The first problem is that when the report expert creates a
report, it doesn't always place the controls on the grid lines. When you later want to rearrange the
controls on the report, or add more controls, you can't get the new objects to line up because they are
snapping to the grid lines. Unfortunately, since the existing objects weren't placed on the grid lines by
the report expert, you can't line them up. The second problem with using the snap-to grid lines option is
that it can be impossible to move multiple objects and keep their spacing consistent. I call this the
"rubber band effect". As the objects move across the report, they seem to be connected by a rubber band.
Some will move at first, and others won't. But as you move your mouse, the ones that haven't moved
will now seem to snap into place and catch up with the other objects. If you move your mouse fairly
quickly across the page they will appear to bounce around like they are being pulled by rubber bands.

http://www.WillyDev.Net
CR.NET Book by Bischof- Chapter 2 - Copyright 2003 Page 21 of 32

Unfortunately, you have no control over this and it can be very difficult to make all the objects line
up as you expected.
An easy way to align multiple objects is to use the Align menu option. After selecting the objects
right-click on one of them and select Align. The options to choose from are Tops, Middles, Bottoms,
Baseline, Lefts, Centers, Rights, and ToGrid. Each of these options aligns multiple objects to a single
object. The object that is used as the basis for alignment is the last object selected. You can identify the
last object selected by seeing which one has the sizing handles on it.

String output can be formatted in a way similar to formatting a cell in a spreadsheet. You can modify its
font and border. There are also formatting options that are specific to Crystal Reports. These options
consist of suppressing the field, letting the width grow, rotating the text, paragraph specific formatting
and hyperlinks. Almost every option on this dialog box can be set using formulas. This is discussed in
more detail in Chapter 7. The formatting options are available for textbox objects, formulas, report
fields, and special fields. Access the formatting dialog box by right-clicking on the field and selecting
Format.
The first tab of the Format dialog box is the Common tab (shown in Figure 2-18). The properties
shown on this tab are common to most of the report objects available. These properties are described in
Table 2-3.

Figure 2-18. The Common tab.

http://www.WillyDev.Net
CR.NET Book by Bischof- Chapter 2 - Copyright 2003 Page 22 of 32

Table 2-3. Properties of the Common Tab


Property Description
Can Grow Allow the field to expand if the object isn't big enough
to hold the data. The field will only expand vertically
and result in the height increasing. The width does not
expand.
Close Border on Page Break If a field has a border, and the field extends to another
page, then this will close the border on the first page.
Horizontal Alignment Sets the alignment to Left, Right, Center or Justified.
Keep Object Together Do not let the object cross over into another page.
Suppress Hides the object.
Suppress If Duplicated Hide the object if it had the same value in the prior
record.
Text Rotation Rotate the text to a specified angle. A value of 0 is the
default and the text displays horizontally. A value of
90 rotates it vertically upward. A value of 270 rotates
it vertically downward.
Tool Tip Text Use the formula editor to set a string that is displayed
when the mouse hovers above the field.
The next two tabs on the Format dialog box are the Border tab and the Font tab. Both of these tabs
are simplistic and don't have anything unusual in them. The Border tab lets you specify which sides
should have a border and it also lets you can change the shading around the object. The Font tab has
properties to change the font and use different effects such as strikethrough and underline.
The third tab of the dialog box is the Paragraph tab. This is useful when using multi-line objects and
you want it to be formatted like a standard paragraph. It is shown in Figure 2-19. It lets you customize
the indentation on the left and right sides. You can also specify a different indentation for the first line
so that it is inset from the rest of the lines. You can also set the line spacing, the reading order (left to
right or vice-versa), and specify if it uses plain text or text formatted as RTF.

http://www.WillyDev.Net
CR.NET Book by Bischof- Chapter 2 - Copyright 2003 Page 23 of 32

Figure 2-19. The Paragraph tab.


The last tab is the Hyperlink tab. This is used for fields that store text to be used as a hyperlink to a
website, an email address, a file, or another report. When you select what type of hyperlink it is, the
middle frame changes so that you can enter the appropriate type of link. You can either hardcode the
hyperlink or use the formula editor to make it dynamic depending upon what is in the field. Depending
on the type of object, some of the hyperlink may be unavailable.

( & '
The text object is used to display text, database fields, and special report fields. Each text object can
display one of these or a combination of all three. After adding a text object to the report, click on it to
edit it.
When writing form letters, using the textbox object with a combination of text and database fields is
very helpful. Text in form letters is different than standard reports because it doesn't follow the standard
format of showing individual rows and columns. It is displayed in paragraph format with a single space
between each word/number. The textbox makes this possible by automatically trimming the spaces
around each word and number. Thus, you can insert database fields in with standard text. Any extra
spaces around the database fields are not be shown. If you have ever programmed in HTML, then you
are familiar with this concept. Within HTML, no matter how many spaces you put between words, they
will be separated by only one space.
The properties that apply to the Textbox object are covered in the section Formatting Text Objects.

http://www.WillyDev.Net
CR.NET Book by Bischof- Chapter 2 - Copyright 2003 Page 24 of 32

& '
The standard field object displays data from a table or a formula. The field object is added to your
report by dragging and dropping it from the Field Explorer window onto your report. The Field Explorer
window is normally docked on the toolbar to the left side of the screen. If you don't have it there, you
can pull it up by clicking on the menu items and selecting View/Other Windows/Document Outline. The
properties that apply to the field object are covered in the Formatting Text Objects section.

& '
The line object does exactly what you expect. It draws a line. There isn't a whole lot you can do with this
except change change its color, the width and its style (single line, dashed line, etc.) It does have one
interesting feature that solves a common problem with line objects. The problem occurs when you draw
a vertical line on a detail section. There are times when the detail section has a field that can grow down
the report and make the section longer than expected. You expected the line to be unbroken down the
report and now that isn't the case because the line is too short. To fix this, set the property
ExtendToBottomOfSection to True. This insures that no matter how short your line is, the end point will
be the bottom of the section. If it is a horizontal line, this always move to the bottom of the section.

) (& '
Like the Line object, the Box object isn't too exciting. You can change its color, width and style. One
nice feature about it is that you can change its properties to round the edges. Depending on how you set
the properties, you can make it elliptical or even turn it into a circle. The properties that effect this are
CornerEllipseHeight and CornerEllipseWidth. Rather than modify these directly using the Properties
window at the bottom right hand corner of the screen, it is much easier to use the Format dialog box.
The second tab is the Rounding tab. It has a picture of what the box looks like and below it is a slider.
As you move the slider from the left to the right, it increases the curvature of the edges. When the slider
is at the far right, the box has turned into a circle. Once you click OK, the dialog box modifies the corner
ellipse properties for you.

+ * ,
-

* & '
The picture object is used for displaying the following image file formats: BMP, JPG, TIFF, and PNG. It
doesn’t display GIF files. The formatting options are similar to the other controls in that it has the tabs
Common, Border, and Hyperlink with similar functionality. There is a Picture tab, shown in Figure 2-
20, that lets you resize, scale and crop the image. If at any point you feel that you resized or scaled it
improperly and you want to restore it back to its original size, click the Reset button.

http://www.WillyDev.Net
CR.NET Book by Bischof- Chapter 2 - Copyright 2003 Page 25 of 32

Figure 2-20. The Picture tab of the Picture object's dialog box.

& '
The Chart object lets you add a chart to your report. There are a variety of options to choose from so that
you can customize however you need to. There is so much to cover with this object that it was given a
separate chapter. See Chapter 11 for more information.

& & '


The OLE object lets you embed objects inside of your report. Some examples are embedding Word
documents or Excel spreadsheets. This is a feature that was brought forward from previous versions of
Crystal Reports is isn't used much anymore.

+ & '
The Hyperlink object is used for giving your report the ability to show a hyperlink to an on-demand
subreport. When a user clicks on the hyperlink, the subreport opens in the viewer and gets its own tab.
This creates an on-demand supreport. The user can toggle back and forth between the two tabs to see
both reports.

. / + *
! /
& 0 +

http://www.WillyDev.Net
CR.NET Book by Bischof- Chapter 2 - Copyright 2003 Page 26 of 32

! 1

& '
The Special Field object is used for printing all report-related information. For example, it can print the
current page number or the total number of pages. Since there isn't a way for you to calculate this
information yourself, this field was created as a catch-all for all the miscellaneous types of report
information that you need. Table 2-4 lists the available fields and what they mean. These fields are
treated the same as a database field object. They can be added to the report by themselves, or included in
a textbox object. They also have all the same formatting options that were described in the Formatting
Strings section.
Table 2-4. Special Fields.
Special Field Description
DataDate The date when the report was last refreshed.
DataTime The time when the report was last refreshed.
FileAuthor The file author that is stored with the report.
FileCreationDate The date when the report was created.
Filename The report's file name.
GroupNumber The current group number.
GroupSelection The group selection formula.
ModificationDate The date the report was last modified.
ModificationTime The time the report was last modified.
PageNofM Prints "Page X of Y".
PageNumber The current page number.
PrintDate The date the report was printed.
PrintTime The time the report was printed.
RecordNumber The current record number.
RecordSelection The record selection formula.
ReportComments The report comments that are stored with the report.
ReportTitle The report title.
TotalPageCount The total number of pages.

Creating the report file makes it possible to use the report in a variety of ways. You can either preview it
with the CrystalReportViewer (Windows or Internet based) or you can print it directly via a command
button or menu item on the form. You also have the option of using the report within the project it was
created in or calling it from a separate application. Using the report file with your application consists of
the 3 steps listed in Table 2-5.
Table 2-5. Steps for printing and previewing a report
1. Adding the CrystalReportViewer If you want to let the user preview the report before
printing it, add the CrystalReportViewer to a form.
This is optional.
2. Binding the report Bind the report class to a form or menu option so it can
be printed.
3. Coding the report methods Print the report using the PrintToPrinter() method. If
you are using the CrystalReportViewer, it will manage
previewing and printing the report for you.

[1]
Adding the CrystalReportViewer Control
Most applications give the user the ability to preview a report before printing it. With Crystal Reports,

http://www.WillyDev.Net
CR.NET Book by Bischof- Chapter 2 - Copyright 2003 Page 27 of 32

the CrystalReportViewer control is the only control that can do this. It must be added to a form to be
used. If you want to print the report directly without previewing it (needed for batch operations) you can
do so without using the viewer. See the section Printing and Previewing the Report for more
information. Figure 2-21 shows the viewer previewing a report.

Figure 2-21. The CrystalReportViewer previewing a report.


Along the top of the viewer is a toolbar with a variety of navigational buttons. In the center is the
report preview window with a Group Tree window to the left of it. Along the bottom is a status bar that
shows the page number information and the current zoom factor.

2 3 & 0 4

By default, the toolbar buttons and Group Tree window are all turned on so that a user has access to
the complete set of features. Each of these features has a corresponding property that can be set in
design mode and during runtime so that you can turn them on or off. You may want to turn off certain
features if they don't apply to your report. For example, a report that doesn't have any groups certainly
doesn't need the Grouping Tree window available. You may also want to turn off all these features so
that you can create a customized preview form. Having a customized preview form lets you control the
user interface by using your own style of buttons. This gives your application a consistent look and feel.

' )

http://www.WillyDev.Net
CR.NET Book by Bischof- Chapter 2 - Copyright 2003 Page 28 of 32

Table 2-6 lists the properties the CrystalReportViewer and Table 2-7 lists its methods. Most of these
are self-explanatory, but the tables make good reference material. The properties let you enable or
disable the features of the viewer. The methods let you implement the viewer's functionality by calling
them from you own buttons.
Table 2-6. CrystalReportViewer Properties
Property Description
DisplayBackgroundEdge A background edge creates a border around the edge
of the report page in preview mode. Setting this to
False makes the edge of the page flush against the
viewer's window.
DisplayGroupTree Toggles the Group Tree window on and off.
DisplayToolbar Toggles the toolbar on and off.
EnableDrillDown Sets whether the user can drill down on reports.
ShowCloseButton Sets whether the Close button is available.
ShowExportButton Sets whether the Export button is available.
ShowGotoPageButton Sets whether the GotoPage button is available.
ShowGroupTreeButton Sets whether the GroupTree button is available.
ShowPrintButton Sets whether the Print button is available.
ShowRefreshButton Sets whether the Refresh button is available.
ShowTextSearchButton Sets whether the Text Search button is available.
ShowZoomButton Sets whether the Zoom button is available.
Table 2-7 CrystalReportViewer Methods
Method Description
CloseView() Pass a null value to close the current view. Pass a view
name to close a specific view.
DrillDownOnGroup() Drill down on a specific group. See Chapter 12 for
details on implementing this method.
ExportReport() Show the Export dialog box.
PrintReport() Show the Print dialog box.
RefreshReport() Refresh the report view. The user will be prompted for
the parameters and logon information again.
SearchForText() Pass a string to search for. If found, it returns True and
moves to the page that has the string.
ShowFirstPage() Move to the first page of the report.
ShowGroupTree() Shows the Group Tree window. It doesn't take any
parameters. There is no corresponding method to hide
it.
ShowLastPage() Move to the last page of the report.
ShowNextPage() Move to the next page of the report.
ShowNthPage() Pass an integer to move to that page number.
ShowPreviousPage() Move to the previous page of the report.
Zoom() Pass an integer to set the zoom level.

Binding the Report


A report is a file that is saved separately from the CrystalReportViewer. The report designer that you use
to create and save the report file is not available at runtime. Since the form and the report are not the
same, you need a way to tell the viewer which report it needs to load. This is called binding the report
and it can be done many different ways. The best method will depend upon your application's
environment and it can also just be based on personal preference.
There are two decisions you have to make when binding to a report. The first decision is determining

http://www.WillyDev.Net
CR.NET Book by Bischof- Chapter 2 - Copyright 2003 Page 29 of 32

the report type. A report can be Untyped or Strongly-Typed. The second decision is whether to use
the ReportDocument component or not. This section shows you how each decision effects your
application. Once you've made these two decisions, the sample code at the end of this chapter shows
youhow to bind the report for the choices you made.

The way that you add a report to a project determines its type, and its type determines how you bind
the report. There are two types of reports: Untyped, and Strongly-Typed. Untyped reports are external
reports that were not created within your project. They could have been written by the Crystal Reports
end-user application, or written by a different .NET application. Since they are not part of the current
project, they have to be referenced by their file path. Strongly-Typed reports have been added to your
application and are listed in the Solution Explorer window. They are an embedded resource and they are
referenced by their class name.
If you are using the CrystalReportViewer, the property that is used for binding the report to the
viewer is the ReportSource property. It needs a reference to the report class so that it can show it in your
application. Since a report is a file on your hard drive, you can assign the fully qualified file name (as a
string) to the ReportSource property. The viewer will find the report file, read it, and display it to the
user. The only problem with this approach is that when you deploy your application to another user's
computer, you must insure that the report is in the same location on their hard drive. If the viewer
doesn't find the report at its fully qualified path, then it will trigger an error and fail. As an alternative to
hard coding the file path during design mode, your application could implement a generic report viewer
that uses the OpenFile dialog box to let the user browse to the report that they want to print.

'* % 5
6
7 ! 8 #
0
Listing 2-1. Setting the filename using the application path.
* 1 0
! 9 : , 5
6% ;<
=0 = 0 <
The second way to reference a report is to treat is like any other class in your application. Create an
object variable and use it to instantiate an instance of the report class. Then assign this object variable to
the ReportSource property of the viewer. Using the class name is better than using the file path because
you know that the class name is always accessible from within your application. The drawback is that
you must limit the reports that a user prints to the ones that are in your application.

Each of the two options has benefits and drawbacks. Using the file path is good for giving users the
ability to pick the report they want to print. This is useful in environments where there is a library of
available reports on a common network drive. Instantiating an object variable is useful when the user
will only print reports that have been written for your application. Of course, it is perfectly acceptable to
use both methods in the same application and give the user different menu options for each.

7
0 3 + & 0 4
+

http://www.WillyDev.Net
CR.NET Book by Bischof- Chapter 2 - Copyright 2003 Page 30 of 32

When binding a report, you have the option of setting all binding and printer properties via code, or
using a visual interface to do so. The ReportDocument component is used for giving you that visual
interface. If you don't add a ReportDocument component to your form, then all the properties must be
set with code. Each method has its pros and cons.
Using the ReportDocument component certainly makes it easier to bind your report. Rather than
writing code to set all the properties, you simply set the properties using the Properties Window. This
also makes it easy for you to see what the properties are without having to go into the View Code
window. The drawback is that the properties you set can only be applied to the report it is associated
with. Each ReportDocument component can only be used with one report. Thus, you need a separate
component for every report.

0 3 &

The drawback to not using the ReportDocument component is that coding requires more work. You
have to declare a report object, instantiate it, and set its properties. However, like most aspects of
programming, having to do more work is compensated by giving you more flexibility. You get the
benefit of sharing this code among a variety of reports. Consolidating your code into a centralized
location makes it easier to maintain.
If you have a lot of reports that use the same printer properties, you are probably better off by not
using the ReportDocument component for them. For your reports that have special printing
requirements, you should add a ReportDocument component for each one. For example, let's say that
most of your report are printed in portrait mode with the same margins, and there are also two reports
that use landscape mode. You can use a common procedure to set these properties for the reports that
use portrait mode. The two other reports that use landscape mode will use the ReportDocument
component to set their properties.

Coding the Report Methods


Writing the code to print and preview the report is the final step of report design. You can print reports
directly with your application or let the user preview the report and then decide whether or not to print it.
If you want the user to be able to preview the report first, your form needs to use the
CrystalReportViewer (see the earlier section relating to this). The viewer takes care of printing the report
once the user initiates the print process.
If you want to print the report without previewing it first, call the PrintToPrinter() method of the
report document. This sends the reports directly to the printer. This is commonly done when printing a
single report with via a menu option or when printing a large volume of reports in a batch mode. The
parameters of the PrintToPrinter() method are listed in Table 2-7.
Table 2-7. PrintToPrinter() parameters.
Parameter Description
NPages The number of pages to print.
Collated Set to True to collate the pages.
StartPageN The first page to print. Set to 0 to print all pages.
EndPageN The last page to print. Set to 0 to print all pages.

Coding Samples
The following code samples are categorized according to the different decisions you made above. The
first decision is whether the report is Untyped or Strongly-Typed. The second decision is whether to set
the properties using code or using the ReportDocument component. The following example code shows
every combination of these possible decisions. Where you put this code in your form depends upon how

http://www.WillyDev.Net
CR.NET Book by Bischof- Chapter 2 - Copyright 2003 Page 31 of 32

you want to display the report. If you have a form whose only purpose is to preview a report, you can
put the code in the form's Load() event. If you want to send the report directly to the printer after the
user sets the print parameters, then put it in a button's Click() event.
Within some of this example code are comments telling you where to set the printer's properties.
This example code isn't shown here because it is discussed in detail in Chapter 19.

, -
Call the Load() method of the ReportDocument component. Pass it the fully qualified file path. Assign
the ReportDocument object to the ReportSource property of the viewer.
Listing 2-2. Use the viewer to preview a report when the form is loaded.
Private Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
reportDocument1.Load("C:\MyReport1.rpt")
CrystalReportViewer1.ReportSource = reportDocument1
End Sub
Listing 2-3. Print a report directly to the printer when a menu option is selected.
Private Sub MenuPrint_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MenuPrint.Click
reportDocument1.Load("C:\MyReport1.rpt")
reportDocument1.PrintToPrinter(1, False, 0, 0)
End Sub

-
ReportDocument components generate all the necessary code for declaring and instantiating the report
object for you behind the scenes. There is only one line of code you have to write. Assign the report
object to the ReportSource property of the viewer.
Listing 2-4. Use the viewer to preview a report when the form is loaded.
Private Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
CrystalReportViewer1.ReportSource = reportDocument1
End Sub
Lising 2-5. Print a report directly to the printer when a menu option is selected.
Private Sub MenuPrint_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MenuPrint.Click
reportDocument1.PrintToPrinter(1, False, 0, 0)
End Sub

, ,
If you are using the viewer, you have the option of setting the file path directly by using the Properties
window or setting it in code. If you aren't using the viewer, you have to instantiate a report object and
call its Load() method. If you set the PropertySource property in the Properties window, then no coding
is required. The viewer will load the report automatically.
Lising 2-6. Uses the viewer to preview a report when the form is loaded.
Private Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
CrystalReportViewer1.ReportSource = "C:\MyReport1.rpt"

http://www.WillyDev.Net
CR.NET Book by Bischof- Chapter 2 - Copyright 2003 Page 32 of 32

End Sub
Listing 2-7. Prints a report directly to the printer when a menu option is selected.
Private Sub MenuPrint_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MenuPrint.Click
Dim myReport As New ReportDocument()
myReport.Load("C:\MyReport1.rpt")
'Set any printer specific properties here
myReport.PrintToPrinter(1, False, 0, 0)
End Sub

,
The code to instantiate the object and print or preview it is your responsibility. Create an object variable
from the report class and instantiate it.
Listing 2-8. Use the viewer to preview a report when the form is loaded.
Private Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Dim myReport As New CrystalReport1()
'Set any printer specific properties here
CrystalReportViewer1.ReportSource = myReport
End Sub
Listing 2-9. Print a report directly to the printer when a menu option is selected.
Private Sub MenuPrint_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MenuPrint.Click
Dim myReport As New CrystalReport1()
'Set any printer specific properties here
myReport.PrintToPrinter(1, False, 0, 0)
End Sub

Writing reports requires many skills. As a .NET developer, you are already familiar with how to build
forms and add code to the controls on the form. These skills carry forward to creating reports because
the report designer is integrated into the Visual Studio IDE. But writing reports requires learning how
common controls can behave differently than what you expect from creating forms. This chapter gave
you an understanding of the fundamental steps for writing reports. Now you are ready to move to the
advanced concepts of report writing and look at each concept in detail.

The following code listings perform the same functionality as the VB .NET code listing presented
throughout this chapter. They are shown here so that the different languages do not complicate the text
by making you decide which code listings to read. Each listing number corresponds exactly to VB .NET
listing numbers.

[1]
This section discusses the CrystalReportViewer control for Windows. ASP.NET develepment is discussed in Chapter X.

http://www.WillyDev.Net

You might also like