Author: Li honggen Microsoft MVP E-MAIL: lihonggen0@163.com
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.
1. register your own crystal report. Otherwise, you can only use it 30 times.
Crystal Report registration code
Registration number: 6707437608
Password: aap5gks0000gde100ds
Ii. Use crystalreportviewer for preview
The crystalreportviewer control allows you to view Crystal Reports in applications. 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. Adjust the Windows form viewer to the desired size and move it to the desired location through the drag-and-drop operation.
3. When an application is running, the report is displayed in the viewer.
3. Create a new report
1. Point to "add" and click "Add new item ".
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 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, complete the operations on the "Report experts" tab, and click "finish" to access the Crystal Report designer and your reports.
4. Do I need to dynamically set the data source?
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 using this model, the connection to the database and the SQL commands executed to obtain data are both processed by Crystal Reports, and developers do not need to write 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
Please fill in the dsdataset dataset by yourself
'Use the report engine object model to pass the filled dataset to the report.
Orpt. setdatasource (dsdataset. Tables (0 ))
'Bind a report object with data to Windows form viewer, 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. Then specify a result table in the filldataset method.
6. Create a master-slave report
In a report, many reports are in the master-slave table structure, such as order and order item details. Orders are one record in one table, while entries are multiple records in another table, two tables are joined by 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. Adjust the position and width of the fields to be displayed in the report designer.
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"
Summary: Crystal Reports have powerful functions. They can also export files such as Word, Excel, and RTF, and generate complex and beautiful charts. They are a powerful tool for Web and Windows report development.
(This article is based on msdn)
Author: Li honggen
E-MAIL: lihonggen0@163.com