http://www.cnblogs.com/babyt/archive/2008/01/03/1024941.html
前文《在水晶报表中实现任意选择指定字段显示》中有朋友留言说是有无C#版本,最近有时间,重写了一下。
1
using
CrystalDecisions.Shared;
2 using CrystalDecisions.CrystalReports.Engine; 3 using CrystalDecisions.Windows.Forms; Click事件代码:
1
private
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 } |
水晶报表公式化字段,自定义报表
最新推荐文章于 2020-03-24 16:29:39 发布