C # Dynamic Data Binding of the crystal report (push mode, non-associated database)

Source: Internet
Author: User

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.

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.