在水晶报表中实现任意选择指定字段显示-模板及C#升级版
前文《在水晶报表中实现任意选择指定字段显示》中有朋友留言说是有无C#版本,最近有时间,重写了一下。
一是使用了VS2005 C#2.0 + Crystal Report 2008,另外就是把模板的实现方式也更改了一下。
在这里就不一步一步讲了,模板的实现方式和xsd文件及mdb文件,请从《在水晶报表中实现任意选择指定字段显示》一文中下载
不过模板在这里做一下说明,本文中的模板中的表头部分,未使用参数字段,而是使用了公式字段。
如下图所示
最终效果为
各控件都使用了默认名称,主要的事件为Click
CR2008自动增加如下代码
一是使用了VS2005 C#2.0 + Crystal Report 2008,另外就是把模板的实现方式也更改了一下。
在这里就不一步一步讲了,模板的实现方式和xsd文件及mdb文件,请从《在水晶报表中实现任意选择指定字段显示》一文中下载
不过模板在这里做一下说明,本文中的模板中的表头部分,未使用参数字段,而是使用了公式字段。
如下图所示
最终效果为
各控件都使用了默认名称,主要的事件为Click
CR2008自动增加如下代码
1using CrystalDecisions.Shared;
2using CrystalDecisions.CrystalReports.Engine;
3using CrystalDecisions.Windows.Forms;
2using CrystalDecisions.CrystalReports.Engine;
3using CrystalDecisions.Windows.Forms;
Click事件代码:
1private void button1_Click(object sender, EventArgs e)
2 {
3
4
5 String cnstr = "";
6 //保持字段的字符串
7 String fldstr = "";
8 //保持字段名称的数组
9 String[] fldArr=new string[6] ;
10 int i = 0;
11 int j = 0;
12
13
14
15 //---------------------------------------------------------------------
16 //获取选取的字段并进行处理
17 if (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 }
2 {
3
4
5 String cnstr = "";
6 //保持字段的字符串
7 String fldstr = "";
8 //保持字段名称的数组
9 String[] fldArr=new string[6] ;
10 int i = 0;
11 int j = 0;
12
13
14
15 //---------------------------------------------------------------------
16 //获取选取的字段并进行处理
17 if (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 }
使用公式构造表头字段节约了不少代码,呵呵。
当然这只是个程序模型~~
-EOF-