Dynamic Crystal Report: any table, any column, and dynamic grid line (transferred from csdn)

Source: Internet
Author: User
This article does not have any technical skills, but mainly focuses on Implementation ideas.
The text version of this article is released in: http://www.cnblogs.com/babyt/archive/2009/04/08/1431328.html

Indicate the source for reprinting.
========================================================== ======================================

I have been asked many times:
Why is it so troublesome for a crystal report? Do you have to select a table and draw lines and grids for every report? Is there a way to make it happen once and for all?
Create a template and use different tables?
In addition, can I dynamically select columns and the report automatically applies to this change after selecting columns?
Some people even give up the Crystal Report. In fact, these are what the crystal report can do.

I have written several articles in scattered ways before, and I have repeatedly explained ideas in forums and blogs, but I have not systematically solved this problem. Here, let me explain the principles and implementation.
I hope this article can solve this problem. Of course, this is not really omnipotent, but also has its applicable environment and limitations.
This article is applicable to common list (table) list display, not suitable for complex formats and operations on templates.
Development Environment: VS2005/cr xi R2 for. Net

Step 1: Create a blank WinForm project for the C # Crystal Report. When the report data source selection box is displayed, do not select any data source. Click OK to create a blank report.

Step 2: Add a Dataset1.xsd to the project, create a DataTable manually, and name it BigTable. Create six fields, all of which are String type.
Why six? This number 6 is based on your actual needs. The number of columns in this table must be greater than or equal to the maximum number of columns in the fact table you need to operate on.
Why is String used? We know that basically all basic data types can be converted to String, so that our table can basically accommodate all data types.

Add this Bigtable to the report.

Drag the six fields to the details section. Alignment.

Note! Do not use boxes and lines to draw a grid. The field border is used here. You can skip this operation for the first time. I will discuss how to deal with this grid line later.

Well, our omnipotent template is made. Simple, right? Haha.

Step 3: drag a combox and a button on winform and use the default name.

Combox1 is used to select the table name.

This interface is complete.

Step 4: Implementation Principle

After the above steps, experienced friends basically showed that we were using the PUSH mode.
Since BigTable has been used in the report, the data to be passed to the report must be converted into BigTable.
That is to say, as long as the data in the source data table is converted into a BigTable, it can be displayed using this template.
You don't have to worry about where the source table comes from, what the table name is, how many fields there are (but the number of fields must be less than or equal to 6), what the field name is, and how much data it has.
To achieve the "omnipotent" purpose.

I encapsulated this job and made a small method.

C # code
   Class clsDyCrystalReportCore {/// <summary> /// convert all the data required for converting the input able into a report template to string /// </summary>/ // <param name = "dt"> source table </param> // The datatable required by the <returns> Report Template </returns> public DataTable dtx (DataTable dt) {DataSet1.BigTatableDataTable dtx1 = new DataSet1.BigTatableDataTable (); object [] obj = new object [dt. columns. count]; // note: the number of columns in the selected table must be <= the number of fields in Bigtable // enter the protection code for (int I = 0; I <dt. rows. count; I ++) {dtx1.Rows. add (dtx1.NewRow (); for (int j = 0; j <dt. columns. count; j ++) {dtx1.Rows [I] [j] = dt. rows [I] [j]. toString () ;}} return dtx1 ;}}

The source table is written to the BigTable according to the BigTable structure. Very simple code, but it is also the core of this solution.

Step 5: implement the function.

C # code
   Using System; using System. collections. generic; using System. componentModel; using System. data; using System. text; using System. windows. forms; using CrystalDecisions. shared; using CrystalDecisions. crystalReports. engine; using CrystalDecisions. windows. forms; using System. data. oleDb; namespace DyCrystalReportDemo {public partial class Form1: Form {public Form1 () {InitializeComponent ();} private void button#click (Object sender, EventArgs e) {String tblName = comboBox1.Text; String connstr = "Provider = Microsoft. jet. OLEDB.4.0; Data Source = "+ System. threading. thread. getDomain (). baseDirectory + "bbtcrall. mdb "+"; "; if (tblName =" ") {MessageBox. show ("Select Table Name"); comboBox1.Focus (); return ;}// open the database connection DataTable dt1 = new DataTable (); DataTable dtx = new DataTable (); oleDbDataAdapter da = new OleDbDataAdapter (); OleDbConnec Tion cn = new OleDbConnection (connstr); // open the selected table (pay attention to error protection) // if you want to implement any column, you only need to change the SQL statement to a specific field. da = new OleDbDataAdapter ("SELECT * From" + tblName, cn); da. fill (dt1); // process ds1 clsDyCrystalReportCore xCore = new clsDyCrystalReportCore (); dtx = xCore. dtx (dt1); ReportDocument myReport = new ReportDocument (); string reportPath = System. threading. thread. getDomain (). baseDirectory + "crystalreport1.rpt"; myReport. load (ReportPath); // bind a dataset. Note that a report uses one dataset. MyReport. setDataSource (dtx); crystalReportViewer1.ReportSource = myReport; crystalReportViewer1.RefreshReport ();} private void Form1_Load (object sender, EventArgs e) {comboBox1.Items. add ("Test1_1"); comboBox1.Items. add ("Test1_2 ");}}}

Others:

1: I will improve the processing of the grid line later.
2: dynamic implementation can also be added using the API of development edition or Crystal Report 2008, but it is much more troublesome than this.
3: This example does not apply to operations on large data volumes.

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.