水晶報表公式化欄位,自定義報表
阿新 • • 發佈:2019-02-01
{
3
4
5 String cnstr ="";
6 //保持欄位的字串 7 String fldstr ="";
8 //保持欄位名稱的陣列 9 String[] fldArr=newstring[6] ;
10 int i =0;
11 int j =0;
12 1314
15 //---------------------------------------------------------------------
16 //獲取選取的欄位並進行處理 17if (checkBox1.Checked ==true)
18 fldstr = fldstr +","+ checkBox1.Text;
19 if (checkBox2.Checked ==true)
20 fldstr = fldstr +","+ checkBox2.Text;
21 if (checkBox3.Checked ==true)
22 fldstr = fldstr +","+ checkBox3.Text;
23 if (checkBox4.Checked ==true)
24 fldstr = fldstr +","+ checkBox4.Text;
25 if (checkBox5.Checked ==true)
26 fldstr = fldstr +","+ checkBox5.Text;
27 if (checkBox6.Checked ==true)
28 fldstr = fldstr +","+ checkBox6.Text;
29
30 if (fldstr =="") {
31 MessageBox.Show("請選擇要顯示的欄位");
32 return;
33 }34
35 if (fldstr.Substring(0, 1) ==",")
36 fldstr = fldstr.Substring(1, fldstr.Length -1);
37
38 fldArr = fldstr.Split(new Char[] {','});
39
40 cnstr="Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+ Application.StartupPath +"//BBT_Crystal.mdb" ;
41 OleDbConnection cn=new OleDbConnection(cnstr);
42 cn.Open();
43
44 string sql="";
45 sql=" Select "+ fldstr +" From 材料採購明細" ;
46
47 OleDbDataAdapter da1=new OleDbDataAdapter(sql,cn);
48
49 DataSet ds1=new DataSet();
50 da1.Fill(ds1, "材料採購明細");
51
52
53 CrystalReport1 crReportDocument=new CrystalReport1();
54
55
56 for(i=0;i<fldArr.Length;i++)
57 {
58
59 // 將公式繫結到具體欄位 60 crReportDocument.DataDefinition.FormulaFields["mf"+ (i +1).ToString()].Text ="{材料採購明細."+ fldArr[i] +"}" ;
61 crReportDocument.DataDefinition.FormulaFields["mt"+ (i +1).ToString()].Text ="/"" + fldArr[i] + "/"";
62 }63
64 for (j =i+1; j <=6; j++)
65 {
66 crReportDocument.DataDefinition.FormulaFields["mt"+ (j).ToString()].Text ="";
67 }68
69 crReportDocument.SetDataSource(ds1);
70 crystalReportViewer1.ReportSource=crReportDocument;
71
72 }
3
4
5 String cnstr ="";
6 //保持欄位的字串 7 String fldstr ="";
8 //保持欄位名稱的陣列 9 String[] fldArr=newstring[6] ;
10 int i =0;
11 int j =0;
12 1314
15 //---------------------------------------------------------------------
18 fldstr = fldstr +","+ checkBox1.Text;
19 if (checkBox2.Checked ==true)
20 fldstr = fldstr +","+ checkBox2.Text;
21 if (checkBox3.Checked ==true)
22 fldstr = fldstr +","+ checkBox3.Text;
24 fldstr = fldstr +","+ checkBox4.Text;
25 if (checkBox5.Checked ==true)
26 fldstr = fldstr +","+ checkBox5.Text;
27 if (checkBox6.Checked ==true)
28 fldstr = fldstr +","+ checkBox6.Text;
29
30
31 MessageBox.Show("請選擇要顯示的欄位");
32 return;
33 }34
35 if (fldstr.Substring(0, 1) ==",")
36 fldstr = fldstr.Substring(1, fldstr.Length -1);
37
38 fldArr = fldstr.Split(new Char[] {','});
39
40 cnstr="Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+ Application.StartupPath +"//BBT_Crystal.mdb" ;
41 OleDbConnection cn=new OleDbConnection(cnstr);
42 cn.Open();
43
44 string sql="";
45 sql=" Select "+ fldstr +" From 材料採購明細" ;
46
47 OleDbDataAdapter da1=new OleDbDataAdapter(sql,cn);
48
49 DataSet ds1=new DataSet();
50 da1.Fill(ds1, "材料採購明細");
51
52
53 CrystalReport1 crReportDocument=new CrystalReport1();
54
55
56 for(i=0;i<fldArr.Length;i++)
57 {
58
59 // 將公式繫結到具體欄位 60 crReportDocument.DataDefinition.FormulaFields["mf"+ (i +1).ToString()].Text ="{材料採購明細."+ fldArr[i] +"}" ;
61 crReportDocument.DataDefinition.FormulaFields["mt"+ (i +1).ToString()].Text ="/"" + fldArr[i] + "/"";
62 }63
64 for (j =i+1; j <=6; j++)
65 {
66 crReportDocument.DataDefinition.FormulaFields["mt"+ (j).ToString()].Text ="";
67 }68
69 crReportDocument.SetDataSource(ds1);
70 crystalReportViewer1.ReportSource=crReportDocument;
71
72 }