This article describes the basic usage of reports. Data is extracted from the jobs table in the pubs database and displayed on the report (the pubs database is the built-in database of SQL server ), how to obtain the value of a text object on the report and how to change its value
Follow these steps:
1. Add a new item and select a dataset
2. In the newly added dataset, right-click to add a able, named jobs, and add the columns job_id, job_desc, min_lvl, and max_lvl, which are consistent with the columns in the jobs table.
3. Add a new project, Crystal Report, select Report Wizard (standard), project data, ADO. NET dataset, DataSet1, and jobs to the selected table on the right, and click Finish.
4. double-click the newly added report (CrystalReport1.rpt), select the field resource manager on the left, select the database field, add the four fields of the jobs table to the Section3 details on the right, and select the toolbar on the left, drag a Text object to section2 (header) and name it txtCh
5. Add a new form (Form1), drag crystalReportViewer from the toolbar to the interface, and adjust the size.
6. Add the following code to the form_load event of the form:
Code
1
2 try
3 {
4 DataSet ds = new DataSet ();
5 using (SqlConnection con = new SqlConnection ("server =.; uid = sa; pwd = sa; database = pubs "))
6 {
7 SqlDataAdapter sa = new SqlDataAdapter ("select * from jobs", con );
8 sa. Fill (ds, "jobs"); // bind a dataset
9
10 CrystalReport1 cr1 = new CrystalReport1 ();
11
12
13 // assign a value to the text object added to the report
14 TextObject to = (TextObject) cr1.ReportDefinition. ReportObjects ["txtCh"];
15 to. Text = "hello ";
16
17
18 // obtain the value of the Text object on the report
19 TextObject to3 = (TextObject) cr1.ReportDefinition. ReportObjects ["txtCh"];
20 String s = to. Text;
21
22
23 cr1.SetDataSource (ds); // report associated data source
24
25 this. crystalReportViewer1.ReportSource = cr1; // set the report
26}
27}
28 catch (Exception ex)
29 {
30}
For more information, please join group 74085440!