In the previous article "dynamic Crystal Report: any table, any column, and dynamic grid,
One thing is not fully implemented yet. The table header column is automatically adjusted according to the content, which corresponds to the content table.
We also noticed that in the previous article, the columns below are not displayed because they do not exist, but the content is empty (''). Note that they are empty, it is not a space.
This principle can also be used to extend the header.
This article is followed up and slightly changed
Delete the original header and put six parameter fields, P1, p2... P6, and set borders. Set the alignment with the field.
Then, improve the code of the form.
1 using system;
2 using system. Collections. Generic;
3 using system. componentmodel;
4 using system. Data;
5 using system. text;
6 using system. Windows. forms;
7 using crystaldecisions. shared;
8 using crystaldecisions. crystalreports. engine;
9 Using crystaldecisions. Windows. forms;
10 using system. Data. oledb;
11 namespace dycrystalreportdemo
12 {
13 public partial class form1: Form
14 {
15 public form1 ()
16 {
17 initializecomponent ();
18}
19
20 private void button#click (Object sender, eventargs E)
21 {
22 string tblname = combobox1.text;
23 string connstr = "provider = Microsoft. Jet. oledb.4.0; Data Source =" + system. Threading. thread. getdomain (). basedirectory + "bbtcrall. mdb" + ";";
24
25 if (tblname = "")
26 {
27 MessageBox. Show ("select the table name ");
28 combobox1.focus ();
29 return;
30}
31
32 // open the database connection
33
34 datatable dt1 = new datatable ();
35 datatable dtx = new datatable ();
36 oledbdataadapter da = new oledbdataadapter ();
37 oledbconnection Cn = new oledbconnection (connstr );
38
39 // open the selected table (pay attention to error protection)
40
41 // if you want to implement any column, you only need to change the SQL statement as a specific field.
42 da = new oledbdataadapter ("select * from" + tblname, CN );
43 da. Fill (dt1 );
44
45 // obtain the actual number of columns Based on DTL
46 int Cols = dt1.columns. count;
47
48
49 // process ds1
50 clsdycrystalreportcore xcore = new clsdycrystalreportcore ();
51 dtx = xcore. dtx (dt1 );
52
53 reportdocument myreport = new reportdocument ();
54 string reportpath = system. Threading. thread. getdomain (). basedirectory + "crystalreport1.rpt ";
55 myreport. Load (reportpath );
56
57 // bind a dataset. Note that a report uses a dataset.
58
59 myreport. setdatasource (dtx );
60
61 // set the parameter, that is, the header
62 for (INT I = 1; I <= 6; I ++)
63 {
64 if (I <= Cols)
65 myreport. setparametervalue ("p" + I. tostring (), "column name" + I. tostring ());
66 else
67 // note that this parameter cannot be saved. You must assign a null value to the unused parameter.
68 myreport. setparametervalue ("p" + I. tostring (),"");
69}
70 crystalreportviewer1.reportsource = myreport;
71 // crystalreportviewer1.refreshreport ();
72}
73
74 private void form1_load (Object sender, eventargs E)
75 {
76 combobox1.items. Add ("test1_1 ");
77 combobox1.items. Add ("test1_2 ");
78}
79
80 private void crystalreportviewer1_load (Object sender, eventargs E)
81 {
82
83}
84}
85}
86
Sorry, I am lazy and I am not alignment.
Note:
1: // obtain the actual number of columns Based on DTL
Int Cols = dt1.columns. count;
2: // set the parameter, that is, the header
For (INT I = 1; I <= 6; I ++)
{
If (I <= Cols)
Myreport. setparametervalue ("p" + I. tostring (), "column name" + I. tostring ());
Else
// Note that this parameter cannot be saved and a null value must be given to unused parameters.
Myreport. setparametervalue ("p" + I. tostring (),"");
}
3: // crystalreportviewer1.refreshreport ();
Note that the parameter is commented out here, because the parameter window will pop up again each time you refresh the page.
If you want to use the Parameter Function, try to disable the refresh button on the toolbar. Prevent problems.