Post on 12-Mar-2021
transcript
McGraw-Hill © 2007 The McGraw-Hill Companies, Inc. All rights reserved.9-1
Writing Database Reports Using Crystal Reports
One of the powerful features of Visual Studio is the report designer by Crystal Decisions called Crystal Reports. The Crystal Report Gallery contains wizards to guide you in creating standard reports and forms or you can use an existing report.
Cha
pter
9
McGraw-Hill © 2007 The McGraw-Hill Companies, Inc. All rights reserved.9-3
Objectives
Create a Crystal Reports template and display the report from a Windows Form or a Web FormUse advanced reporting features, such as numeric functions, grouping, sorting and special fieldsBase a report on a data file or a datasetDisplay a report at run time based on a user request
McGraw-Hill © 2007 The McGraw-Hill Companies, Inc. All rights reserved.9-4
About Crystal Reports
Visual Studio 2005 (Professional Edition and above) includes CR.NET 2005 which is based on Crystal Reports Version 10
McGraw-Hill © 2007 The McGraw-Hill Companies, Inc. All rights reserved.9-5
Creating and Displaying a Report
Steps to create and display a simple report
1. Add a Report Designer and design the report template.
2. Add a CrystalReportViewer control to a form and connect it to the report template.
Once a report is added to a project it can be viewed and sent to the printerWhen creating a report template, the format is set up for the report
The data are not included in the report layout
McGraw-Hill © 2007 The McGraw-Hill Companies, Inc. All rights reserved.9-6
Adding a Report Designer
Select Add New Item from theProject menuChoose theCrystal Report templateGive the report a name; the .rpt extension is added by default
McGraw-Hill © 2007 The McGraw-Hill Companies, Inc. All rights reserved.9-7
Report Type OptionsChoose Using the Report Wizard option to open a wizard to step through the report creation processChoose As a Blank Reportto create your own report from scratchChoose From an Existing Report to create a modification of a report from one that is already created
McGraw-Hill © 2007 The McGraw-Hill Companies, Inc. All rights reserved.9-8
Selecting a Data Source
Crystal Reports can connect to several types of dataSQL ServerAccessExcel and others including an ADO.NET DataSet
Dataset can be based on many different types of objects, including business objects and collections
McGraw-Hill © 2007 The McGraw-Hill Companies, Inc. All rights reserved.9-9
Completed Report
McGraw-Hill © 2007 The McGraw-Hill Companies, Inc. All rights reserved.9-10
Selecting a SQL Server Database
Important:File must be installed on the server
McGraw-Hill © 2007 The McGraw-Hill Companies, Inc. All rights reserved.9-11
Selecting an Access Database File
An Access file can be stored in the project folder and is more portable than using a SQL Server database
McGraw-Hill © 2007 The McGraw-Hill Companies, Inc. All rights reserved.
Report Creation WizardSelecting the Tables
9-12
McGraw-Hill © 2007 The McGraw-Hill Companies, Inc. All rights reserved.9-13
Report Creation WizardLinking Tables and Selecting Fields
McGraw-Hill © 2007 The McGraw-Hill Companies, Inc. All rights reserved.9-14
Report Creation WizardGrouping, Sorting and Summarizing
McGraw-Hill © 2007 The McGraw-Hill Companies, Inc. All rights reserved.9-15
Crystal Reports Report Designer
The Crystal Reports report designer with the selected options
McGraw-Hill © 2007 The McGraw-Hill Companies, Inc. All rights reserved.9-16
CrystalReportViewer Control
McGraw-Hill © 2007 The McGraw-Hill Companies, Inc. All rights reserved.9-17
Toolbox and Field ExplorerUse to show and add new fields to a report.
Add lines, boxes, or additional text that is not bound to a data field
McGraw-Hill © 2007 The McGraw-Hill Companies, Inc. All rights reserved.9-18
Modifying a Report Design
Move, resize, reformat fields in the designerTo reformat a field, right-click and select Format Objectfrom the context menu
McGraw-Hill © 2007 The McGraw-Hill Companies, Inc. All rights reserved.9-19
Formula Editor
Use the Formula Editor to create or edit formulas
McGraw-Hill © 2007 The McGraw-Hill Companies, Inc. All rights reserved.9-20
Adding a Special Field
Specify that the subtotal for each group should appear
The subtotal is in a Group Footer section and the grand total in the Report Footer section
McGraw-Hill © 2007 The McGraw-Hill Companies, Inc. All rights reserved.9-21
Report Formatting Modifications
Field Spacing, Alignment, and Column Headings can be modifiedView the report when making modifications to see the result of the modificationAs an option, run the application and set the form’s WindowState property to Maximized to more easily view the entire report
McGraw-Hill © 2007 The McGraw-Hill Companies, Inc. All rights reserved.9-22
Displaying a Report from a Web Form (1 of 2)
Add a CrystalReport Source control from the Crystal Reports section of the toolbox to the Web page
In the control’s smart tag, select Configure Report Source and select the name of the .rpt fileAdd a CrystalReportViewer control and set its ReportSource to the CrystalReportSource that was added
McGraw-Hill © 2007 The McGraw-Hill Companies, Inc. All rights reserved.
Displaying a Report from a Web Form (1 of 2)
Create a SelectedReport property for the ReportFormBegin a new Web Site project and copy the two files for the reportAdd the .rpt file to the project using WebSite/Add Existing ItemAfter adding, the report name appears in the configuration for the CyrstalReportSource control
9-23
McGraw-Hill © 2007 The McGraw-Hill Companies, Inc. All rights reserved.9-24
Basing a Report on a DataSet
A Crystal Report can be based on a dataset either when a new report is created or for an already created report
To display, the report code must be written to fill the dataset and assign the dataset as the data source
McGraw-Hill © 2007 The McGraw-Hill Companies, Inc. All rights reserved.9-25
Report Based on ADO.NET DataSet
McGraw-Hill © 2007 The McGraw-Hill Companies, Inc. All rights reserved.9-26
Selecting From Multiple Reports
In order to pass the selection to a second form, set up a property of the form and create an enum to assist in the selectionWhen a user selects one of the menu items, the event handler must pass the selection to the second form—ReportsFormOn ReportsForm, a single CrystalReportViewercontrol can display the selected report
McGraw-Hill © 2007 The McGraw-Hill Companies, Inc. All rights reserved.9-27
Create a SelectedReport Property for the ReportForm
The MainForm menu commands must pass data to ReportsForm indicating the report to display
' Module-level property declaration.' SelectedReport property of the form.
Private selectedReportType As ReportType
WriteOnly Property SelectedReport() As ReportType' Set SelectedReport property using the ReportType enum. Set(ByVal Value As ReportType)
selectedReportType = valueEnd Set
End Property
McGraw-Hill © 2007 The McGraw-Hill Companies, Inc. All rights reserved.9-28
Moving a Crystal Reports ProjectThe location of the database file may need to be reset unless:
The report is based on a dataset in the current project and assigned at run time – or –The report refers to an instance of SQL Server that can still be accessed from the new location
Right-click in the report’s designer and select Database/Set Datasource location; in the Replace with pane of the Set DataSource dialog box, select Create New Connection and make the same selections to match when the original report was created