Use CrystalReport in WinForm-basic, group statistics, custom data source, and crystalreport

Source: Internet
Author: User

Use CrystalReport in WinForm-basic, group statistics, custom data source, and crystalreport

Opening

This article provides a step-by-step tutorial for beginners who are new to CrystalReport. This tutorial consists of three parts: 1) basic usage of CrystalReport; 2) Grouping and summation of data using CrystalReport; 3) how to use a custom data source (such as a able table in memory) in CrystalReport. I believe that after reading this tutorial, you can easily implement the report function in your project. Creation and display of a CrystalReport Template Create A template1. Right-click [project properties] to add a crystal report (in vs, The CrystalReport module needs to be installed, different vs versions download different CrystalReport version: CrystalReport download link: http://scn.sap.com/docs/DOC-7824) 2. The design page for adding a report is as follows: a report contains five basic parts: (Report header, header, details, report end, and header. 3. Now we need to add a data source to the new report, right-click the database Field on the left and select the corresponding data source for binding. Here I select the sqlserver database. 4. Enter the login information of the database, and then select the database table to be bound. In addition to binding data tables, you can also bind the query results of SQL statements, as well as the stored procedures and views, the following shows the database binding table and the bound SQL query. 1) BIND database table 2) Click Add command below connection to bind an SQL statement. 5. After the database is bound, the corresponding header fields of the table and SQL query results will appear in [field manager] on the left side of the report. 6. layout the corresponding fields in the report view. The template of the crystal report has been completed. Note: The userName, courseName, and score in the header can be changed.  B template display1. Drag a CrystalReportView control on the form as the display report template. 2. Write the following code in the form_load event:
CrystalReport1 cr = new CrystalReport1 (); // instantiate a report template this. crystalReportViewer1.ReportSource = cr; // set the data source for the report Display Control
3. The design of running reports is ugly, but you can perform typographical design based on your own style, such as font, size, and color. 2. CrystalReport group statistics
In the first part, we have achieved the report output of each student's score. Next, we need to make group statistics for each student to obtain the average score of each student. 1. Right-click field resource manager on the left and click Insert group. 2. Select the field to be grouped, click OK. 3. A [group] section is automatically added to the report designer. 4. display the name of each group in the group header. Then, we can calculate the average score at the end of the group. Click RUN total field> New ]. 5. Click OK. The "Run total" field is displayed, and the avg value is set. 6. Click OK. In the "field resource manager" on the left, the "run total field" page displays "avg ]. 7. Next, you can drag this field to the end of the group in the report designer. 8. The report template for grouping statistics is ready. And then display the report template. Three CrystalReport: bind Custom Data Source In step 1 and step 2, we use sqlserver (data tables, query results, and so on) as the data source, both of which are a two-dimensional table, and then use the two-dimensional table header to layout the report template, then the report is automatically connected to sqlserver when it is displayed, and the data is obtained and displayed according to the settings. In this way, we do not have enough control over the data. Sometimes we need to process data in the memory to form a datable table, and then bind it to the report for display. The following shows how to use the memory able table as the data source of the crystal report. 1. Define a able data table and add some data (as a custom data source ). The Code is as follows:
     private DataTable GetCustomDataTable()        {            DataTable dt = new System.Data.DataTable();            dt.Columns.Add("id", typeof(string));            dt.Columns.Add("userName", typeof(string));            dt.Rows.Add("430400","tjm");            dt.Rows.Add("430401", "lw");            dt.Rows.Add("430402", "tj");            return dt;        }
2. After defining the data source, we need to create a report template based on our custom data source. We know from the previous two steps that the design of the Report Template requires fields of the data source. Therefore, we add a dataset to create a report template. 3. Define a virtual table header on the dataset. The virtual table header must be consistent with the data source header defined in the memory to facilitate the report design. 4. In the report designer, add the custom data source we have created and design it. 5. Bind the custom data source for the report in Form_load.
Private void Form1_Load (object sender, EventArgs e) {CrystalReport2 cr = new CrystalReport2 (); cr. setDataSource (GetCustomDataTable (); // data source binding (DataTable type) this. crystalReportViewer1.ReportSource = cr ;}
6. Click Run. The result is the data in the data table we have defined. Summary

I hope you can write this tutorial to help those who are new to the Crystal Report. There may be some errors. please correct me.

 

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.