Because the data in the project comes from the network rather than the database, all tables are dynamically created. Therefore, dynamic creation is also required when using reports.
Example:
1. obtain data under mainform:
Public datatable table = new datatable ("mytable ");
// Create a data table
Public void createdatatable ()
{
Table. Columns. Clear ();
Datacolumn column = new datacolumn ("username", type. GetType ("system. String "));
Column. Caption = column. columnname;
Table. Columns. Add (column );
Table. Rows. Clear ();
For (INT I = 0; I <8; I ++)
{
Datarow newrow = table. newrow ();
Newrow [1] = "name" + I. tostring ();
Table. Rows. Add (newrow );
}
}
2. display the report form:
The constructor that calls a report form with one parameter:
// DISPLAY THE CRYSTAL REPORT
Private void btncrystalreport_click (Object sender, eventargs E)
{
Formcrystal frmreport = new formcrystal (table );
Frmreport. Text = "Crystal Report example ";
Frmreport. windowstate = formwindowstate. maximized;
Frmreport. showdialog ();
Frmreport. Dispose ();
}
3. Report Form Constructor (with 1 parameter ):
Public formcrystal (datatable table)
{
Initializecomponent ();
ReportDocument Doc = new reportdocument ();
String rptname = @ "D: \ C #. Projects \ csprojects \ csreportexam \ crystalreport1.rpt"; // designed report
Doc. Load (rptname );
Doc. setdatasource (table); // sets the data source
This. crystalreportviewer1.reportsource = Doc; // load the report
}
4. Crystal Report design:
(1) place a crystalreportviewer control on the report form and create a report: crystalreport1.rpt;
(2). ApplicationProgramAdd a new project: Data-dataset, save as dataset1.xsd;
(3 ). in the dataset view, add a table. In this case, the main name must be the same as the name of the passed table, named mytable; add a column according to the column in mytable. Here there is only one: username, the names must be consistent. (This is critical. Otherwise, data cannot be displayed in the report..)
(4 ). in the report design view, open "database experts" and select "project data" -- "ADO. net dataset ", add the table to" selected table ", and then return to the view to drag the column (field) to be displayed in the report to the" detailed field "in the report.
In this way, a basic crystal report dynamic data binding is realized.