Usage skills of Crystal Reports

Source: Internet
Author: User

Usage skills of Crystal Reports

Crystal report is an excellent report development tool. When I develop a general management system, all reports use Crystal Reports. Its simplicity, ease of use, and powerful functions have become a favorite of the author, now we will present the crystal report to you by hand.

The

crystalreportviewer control allows you to view the Crystal Report in the Program of the application. The reportsource attribute is used to set the report to be viewed. After this attribute is set, the report is displayed in the viewer. The report source can be a reportdocument or report file path, or a strongly typed report.
1. Open the toolbox and drag a crystalreportviewer to the form. we name it rptvew.
2. Drag and Drop the Windows form viewer to adjust the desired size and move it to the desired location.
3. When an application is running, the report is displayed in the viewer.
3. Create a Report
1. Point to "add" and click "Add new project ".
2. In the "Add new project" dialog box, select Crystal Report from the "template" area, name the report rptclient, and click "open ".
3. in the crystal report library, select one of the following options:
? Use report experts to guide you through the report creation process and add your selection to the Crystal Report designer.
? As a blank report-open crystal report designer.
? From an existing report-create a new report, which is designed to be the same as another specified report.
note that the crystal report library contains many experts who can guide you through the creation of several specific types of reports. You may want to use experts to create the initial report to determine which report construction method is suitable for your needs.
4. Click OK.
if you select "report expert", the "Report expert" dialog box appears with the data resource manager. Select the required data for each folder and complete the operations on the "Report expert" tab, click "finish" to access crystal report designer and your report.
4. Do you need to set the data source dynamically?
Crystal Reports connects to the database through the database driver. Each driver is programmed to process a specific database type or database access technology.
pull and push models
to provide developers with the most flexible data access methods, the Crystal Reports database driver is designed to provide both a pull model and a push model for data access at the same time.
PULL model

In the PULL model, the driver connects to the database and pulls in the data as needed. When this model is used, the connection to the database and the SQL commands executed to obtain data are both processed by Crystal Reports itself, and do not need to be compiled by developers.Code. If you do not need to write any special code at runtime, use the PULL model.
Push Model

On the contrary, the pushing model requires developers to write code to connect to the database, execute SQL commands to create a record set or dataset that matches the fields in the report, and pass the object to the report. This method allows you to place the connection share in the application and filter the data before Crystal Reports receives the data.

4. Create a report from the ADO. Net Dataset
Create a DataSet object from a database
1. Create a new architecture file in the project:
A. in Solution Explorer, right-click the project name, point to "add", and click "Add new project ".
B. In the "category" Area of the "Add new project" dialog box, expand the folder and select "data ".
C. Select "dataset" in the "template" area ".
D. Accept the default name dataset1.xsd.
This creates a new schema file (dataset1.xsd), which will be used to generate a strong dataset. The schema file is displayed in the ADO. Net Dataset Designer.
2. Specify the database location:
A. in server resource manager, right-click "Data Connection" and select "add connection ".
B. In the "Data Link Properties" dialog box, click the "providers" tab and select a provider (for example, Microsoft ole db provider for SQL Server ).
C. Click the connection tab and specify the location of your database. Enter the server and logon information at the required location.
D. Click OK.
In this case, your database, its tables, and fields appear under the "Data Connection" node of the server resource manager.
3. In Solution Explorer, double-click dataset1.xsd (if it is not an active view yet ).
Dataset1.xsd should now be displayed on the dataset tab.
4. To create a schema for a dataset, drag the required table from the server resource manager to the dataset tab of dataset1.xsd.
5. Click "Save dataset1.xsd" to save the "dataset1.xsd" file.
6. On the "generate" menu, click "generate" to generate a DataSet object for the project.
The ADO. Net DataSet object provides a description of the data from which tables can be added to the Crystal Report. Use "database experts" in Crystal Report designer to add tables from ADO. Net dataset objects.
Call "Database Expert" when using "report expert" to create a new report ". Or, you must use ADO. to access "database experts" in a report created by. net, right-click report designer, point to "Database", and click "Add/delete database ".
Connect a report to an ADO. Net DataSet object
1. Expand the project data folder in "database experts.
2. Expand the "ADO. Net dataset" folder.
3. Select the expected DataSet object.
For example, if you are using a DataSet object generated from the project "dataset1.xsd" architecture file "windowsapplication1", you should select "windowsapplication1.dataset1 ".
4. Select the table to be added to the report, just like using other data sources.

5. dynamically change the data source code
dim dsdataset as new dataset ()
dim orpt as new rptclient () 'created report rptclient
ask the reader to manually fill the dataset dsdataset
'use the report engine object model to pass the filled dataset to the Report
orpt. setdatasource (dsdataset. tables (0)
'binds a report object with data to the Windows form viewer and rptvew (crystalreportviewer Control)
rptvew. reportsource = orpt
note that the filldataset method can connect to the specified database, extract data, and then disconnect the database. If you want to add multiple tables in the database to the report, use the SQL join statement to join these tables; specify a result table in the filldataset method
6. Create a master-slave Report
in the report, many reports are in the master-slave table structure, such as order and order item details, an order is a record in a table, and a record is multiple records in another table. Two tables are associated with one field. This report can be implemented using its grouping function.
1. create a project
2. add a crystalreportviewer control to form1
3. connect to the northwind database on SQL Server 2000 in the Service Catalog Resource Manager
4. add a dataset dataset1 and add orders and order details from server resource manager to the dataset.
5. add a crystal report, use report experts, and select "ADO.. Net dataset ", insert the table orders and order details. The" Link "contains links to associated fields. In the" field ", select the fields of the master table and the detail table to be displayed, the ordersid field, total, chart, selection (filtering), and style (report title can be set) of the orders table are selected for the group. After setting, click Finish.
6. In the report designer, adjust the position and width of the fields to be displayed.
7. Add code in the window.

Private sub form1_load (byval sender as system. Object, byval e as system. eventargs) handles mybase. Load
Dim orpt as new crystalreport1 ()
Dim dsdataset as new dataset1 ()

Dim cn as new data. sqlclient. sqlconnection ("Data Source = pmserver; initial catalog = northwind; user id = sa; Password = sa ")
CN. open ()
Dim daorders as new data. sqlclient. sqldataadapter ("select * from orders", CN)
Daorders. Fill (dsdataset, "orders ")

Dim dadetails as new data. sqlclient. sqldataadapter ("select * from [Order Details]", CN)
Dadetails. Fill (dsdataset, "Order details ")

'Use the report engine object model to pass the filled dataset to the report.
Orpt. setdatasource (dsdataset)
Crystalreportviewer1.reportsource = orpt
End sub
8. Run the program

7. Use a program to change the text in the report
The Code is as follows:
Dim gettextobject as textobject
'Get the reportobject by name, convert it to textobject, and return this object.
Gettextobject = orpt. reportdefinition. reportobjects. Item ("text13 ")
Gettextobject. Text = "XXXX System"

conclusion: Crystal Reports have powerful functions. They can also export files such as Word, Excel, and RTF, and generate complex and beautiful charts, is a powerful tool for Web and Windows report development.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.