22
McGraw-Hill © 2010 The McGraw-Hill Companies, Inc. All rights reserved. Chapter 9 Reports

Chapter 9 fileMcGraw-Hill 9-2 Objectives •Create a Crystal Reports template and display the report from a Windows Form or a Web Form •Use advanced reporting features, such as

Embed Size (px)

Citation preview

McGraw-Hill © 2010 The McGraw-Hill Companies, Inc. All rights reserved.

Chapter 9

Reports

McGraw-Hill 9-2

Objectives

• Create a Crystal Reports template and display the report from a Windows Form or a Web Form

• Use advanced reporting features, such as numeric functions, grouping, sorting, and special fields

• Base a report on a data file or a dataset

• Display a report at run time based on a user request

McGraw-Hill 9-3

Writing Reports

• New Project dialog box includes a

Reporting option

– Contains templates for a Crystal Reports

application and a Microsoft Reports

application

– Reporting templates available for Web

applications

• Most popular Visual Studio reporting tool

is Crystal Reports

McGraw-Hill 9-4

Creating and Displaying

a Crystal Report

• Crystal Reports can connect to several

types of data

– SQL Server, Access, Excel, ADO.NET

DataSet, and many others

– An ADO.NET DataSet can be based on many

different types of objects, including business

objects and collections

McGraw-Hill 9-5

Creating a Grouped Report –

Step-by-Step - 1

• Create the project

• Begin the report

– Right-click the project

name in Solution Explorer

– Select Add/New Item from

the context menu

– Select Crystal Report

template from the

Reporting category

– Type a name and click the

Add button

McGraw-Hill 9-6

Creating a Grouped Report –

Step-by-Step - 2

• Select the Report

Wizard and the type of

report

McGraw-Hill 9-7

Creating a Grouped Report –

Step-by-Step - 3

• In the Available Data Sources pane, expand the Project Data node

– Expand the ADO.NET DataSets node

– Add desired tables to the Selected Tables pane

McGraw-Hill 9-8

Creating a Grouped Report –

Step-by-Step - 4

• Wizard pages

– Link • Connect key fields that are not identically named

– Fields • Select fields to display on the detail lines of the

report

– Grouping • Choose which fields to sort and group the report by

– Summaries • Select the fields for subtotals and totals

McGraw-Hill 9-9

Creating a Grouped Report –

Step-by-Step - 5

• More wizard pages

– Group Sorting, Chart, Record Selection

• Press Next on each page

– Report Style

• Click on each entry in the Available Styles box to

see possibilities

• Click Standard and click Finish

• Preview the report

– Shows layout, not actual data

McGraw-Hill 9-10

Crystal Reports Report Designer

McGraw-Hill 9-11

Displaying a Report from a

Windows Form – Step-by-Step - 1

• Drag a CrystalReportViewer control from

the toolbox to the form

– Form and control can be resized and Dock

property of the control can be changed

– Select Choose a Crystal Report from the

report viewer smart tag

• Drop down the list and select the report

McGraw-Hill 9-12

Displaying a Report from a

Windows Form – Step-by-Step - 2

• Write code in the Form_Load event to fill

the dataset

– Declare module-level variables for the dataset

and two table adapters

– Instantiate and fill the table adapters and

dataset

– Assign the dataset to the report

• Run the project

McGraw-Hill 9-13

Using the Report Designer - 1

• Toolbox contains a new

section when the

Crystal Reports

designer displays

• Use the Crystal Reports

Field Explorer to add

new fields to a report

McGraw-Hill 9-14

Using the Report Designer - 2

• Report Header

– Appears one time at beginning of a report

• Page Header

– Appears at the top of each page

• Group Header

– Appears when grouping is selected for a report

• Details band

– Holds data for body of report

McGraw-Hill 9-15

Using the Report Designer - 3

• Group Footer

• Appears at end of each group and

displays subtotals

• Report Footer

– Appears once at end of report, displays totals

• Page Footer

– By default, page numbers appear in this band

at the bottom of each page

McGraw-Hill 9-16

Modifying a Report Design

• Move, resize, and reformat fields in the

designer

– Click on any field and resize using the sizing

handles

– Drag the control to move it

– Right-click and select Format Object to

reformat a field

McGraw-Hill 9-17

Modifying the Products Report

Step-by-Step - 1

• Set up the Report Header

• Fix the date and eliminate the repeating

category names

• Set the page margins

• Sort the report

McGraw-Hill 9-18

Modifying the Products Report

Step-by-Step - 2

• Add a calculated field – Display the Field Explorer

– Right-click on Formula Fields and select New

– Enter a name for the formula and click Use Editor

– Use the Formula Editor to create or edit formulas

– Drag the completed formula from the Field Explorer and drop it in the Details section

McGraw-Hill 9-19

Modifying the Products Report

Step-by-Step - 3

• Add a Special Field

– Expand the Special Fields node in the Field

Explorer

• Examine predefined fields that can be added

• Fix the field spacing, alignment, and

column headings

McGraw-Hill 9-20

Displaying a Report

from a Web Form - 1

• Add a CrystalReportSource control from

the Reporting section of the toolbox

– Select Configure Report Source from the

smart tag

– Select the name of the .rpt file

• Add a CrystalReportViewer control

– Set the ReportSource to the CrystalReport-

Source just added

McGraw-Hill 9-21

Displaying a Report

from a Web Form - 2

• Display a report from a Web Form – Begin a new Web Site project

– Copy the .rpt and .vb files for the report into the project’s folder

– Add the .rpt file to the project using WebSite/Add Existing Item

– The report name appears in the configuration for the CyrstalReportSource control

– If a report is based on a dataset, additional code must be written in the Web page to fill the dataset and set the data source for the report

McGraw-Hill 9-22

Selecting from Multiple Reports

• To pass the selection to a second form, set up a property of the form

– Set up an enum to assist in the selection

• When a user selects one of the menu items, the event handler must pass the selection to the second form

• On ReportsForm, a single CrystalReport-Viewer control can display the selected report