Any non-associated table, any column, and any data source csdn for expansion of the Crystal Report dynamic table)

Source: Internet
Author: User
This article is in <dynamic Crystal Report: any table, any column, and dynamic grid line>
Http://www.cnblogs.com/babyt/archive/2009/04/08/1431328.html
An extension based on the principle.
If you have not read this article before, Please study it carefully. Otherwise, you may not be able to understand it quickly.

Applicable scenarios:
For the purpose of display effect or paper saving, multiple tables with different structures (from different data sources) are displayed in the same report.

Generally, this effect can be achieved through subreports.

However, a subreport usually corresponds to a table, which makes it more difficult to operate multiple tables.

Each table has a subreport, which does not meet the requirements for dynamic table fetch (number of tables, table name.

Although you can also use the SDK to dynamically Add a subreport to different tables, the Code volume is large.

Basic Ideas:
Since we continue the above principles, the main problem of this solution is how to add any number of tables to the same able.

1:
We also need to construct a datatable to accommodate our table data. Suppose the maximum number of columns in our table is 6,

Then we need to create a datatable with seven columns.

Why are there six above and seven here?

This field f0 is used to store the table name. In this way, we can distinguish the table from which the data comes from.

In this way, we have created f0, f1 ~ F6: datatable of 7 String rows.

2:
Let's rebuild the previous example. The template is basically the same. Only f0 is added. However, this f0 is not displayed on the interface, so we still use six grids.

It should be noted that the difference is that the header is no longer displayed on the header. Why is this done? We will discuss it below.

3:
Then, we will transform our core approach.
Before writing data to each table, write a row of Column Titles. This title also replaces our previous header title.

And the first column of each row of data, that is, f0, is written into the table name.

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> // <param name = "tblName"> name of each table </param> // <param name = "fldsName"> Field name, separated by commas. </Param> // <returns> the datatable </returns> public DataTable dtx (DataTable dt, String tblName, String fldsName) {String oneRow = ""; dataSet1.BigTatableDataTable dtx1 = new DataSet1.BigTatableDataTable (); object [] obj = new object [dt. columns. count]; // special note: the number of columns in the selected table must be <= the number of fields in Bigtable // enter the protection code. // The column name must be written to the first line of dtx1.Rows in each table. add (dtx1.NewRow (); // name of the table written in the first column of the First row dtx1.Rows [0] [0] = tblName; // cut column name string, write the position behind the first row (Int I = 0; I <fldsName. split (new char [] {','}). length; I ++) {dtx1.Rows [0] [I + 1] = fldsName. split (new char [] {','}) [I];} // write data for (int I = 0; I <dt. rows. count; I ++) {dtx1.Rows. add (dtx1.NewRow (); // The first column of each row, that is, f0, the name of the written table. Dtx1.Rows [I + 1] [0] = tblName; // write data for (int j = 0; j <dt. columns. count; j ++) {oneRow = oneRow + "," + dt. rows [I] [j]. toString (); if (dt. rows [I] [j]. toString () = "") dtx1.Rows [I + 1] [j + 1] = ""; elsedtx1.Rows [I + 1] [j + 1] = dt. rows [I] [j]. toString () ;}} return dtx1 ;}}

4:
Let's take a look at our front-end code.

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 connstr = "Provider = Microsoft. jet. OLEDB.4.0; Data Source = "+ System. threading. thread. getDomain (). baseDirectory + "bbtcrall. mdb "+"; "; // open the database connection OleDbConnection cn = new OleDbConnection (connstr); DataTable dtxAll = new DataTable (); // accommodate all data OleDbDataAdapter da = new OleDbDataAdapter (); clsDyCrystalReportCore xCore = new clsDyCrystalReportCore (); // note that the number of fields in each table cannot exceed the maximum value we set Number! // Add error protection and object release code by yourself // 1st tables DataTable dt1 = new able (); DataTable dtx1 = new DataTable (); da = new OleDbDataAdapter ("SELECT * From Test1_1", cn); da. fill (dt1); dtx1 = xCore. dtx (dt1, "table 1", "number, name, mail date, other"); dtxAll. merge (dtx1); // 2nd tables. Note that this table can be obtained from different data sources! // Reinitialize dt1 = new able (); dtx1 = new DataTable (); da = new OleDbDataAdapter ("SELECT * From Test1_2", cn); da. fill (dt1); dtx1 = xCore. dtx (dt1, "Table 2", "number, name, employment date, date 1, date 2"); dtxAll. merge (dtx1); // 3rd tables. Note that this table can be obtained from different data sources! // Reinitialize dt1 = new able (); dtx1 = new DataTable (); da = new OleDbDataAdapter ("SELECT * From Test_4", cn); da. fill (dt1); dtx1 = xCore. dtx (dt1, "Table 3", "Year, region, indicator, maximum value, minimum value"); dtxAll. merge (dtx1); ReportDocument myReport = new ReportDocument (); string reportPath = System. threading. thread. getDomain (). baseDirectory + "crystalreport1.rpt"; myReport. load (reportPath); // bind the dataset myReport. setDataSource (dtxAll); crystalReportViewer1.ReportSource = myReport; crystalReportViewer1.RefreshReport ();}}}

Note that we have used

DtxAll. Merge (dtx1 );

Merge all the data into a single able to achieve our initial goal.
5:
Run the following command to make it messy, right?

6:
In this case, f0 is used. Add a f0 group to the template, as shown in.

& Nbsp;

7:
Running up, handsome?
 
Http://topic.csdn.net/u/20090423/01/df01d702-5f29-437b-abb8-107dc3be9b2a.html (csdn)

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.