使用未绑定字段的动态报表,出错。
测试程序如下:(参考CrystalReport官方网站上的VB版未绑定字段的动态报表)
Visual C#的Windows应用程序,数据库为SQL Server2000
一个报表名为CrystalReport1,里面包含两个未绑定字段:“未绑定字符串1”和“未绑定字符串2”
一个窗口上面一个名为CrystalReportViewer1的报表控件,一个按钮。要实现单击按钮后“未绑定字符串1”显示数据库mcms里表TBCUSTOM的ID字段内容,“未绑定字符串2”显示NAME字段。
单击事件如下:
private void button1_Click(object sender, System.EventArgs e)
{
string myConnectionString = "workstation id=YINMIAO;packet size=4096;user id=sa;data source=huijie;persist security info=False;initial catalog=mcms;password=111111";
SqlConnection myConnection = new SqlConnection(myConnectionString);
string mySelectQuery = "select ID,NAME from TBCUSTOM";
SqlDataAdapter myDataAdapter = new SqlDataAdapter(mySelectQuery,myConnection);
DataSet ds = new DataSet();
myDataAdapter.Fill(ds,"TBCUSTOM");
string strReport =@"G:\WindowsApplication10\CrystalReport1.rpt";
ReportDocument report = new ReportDocument();
FormulaFieldDefinition crFormulaTextField1;
FormulaFieldDefinition crFormulaTextField2;
FormulaFieldDefinitions crFormulas;
report.Load(strReport);
report.Refresh();
report.SetDataSource(ds);
crFormulas = report.DataDefinition.FormulaFields;
crFormulaTextField1 = crFormulas[0];
crFormulaTextField2 = crFormulas[1];
crFormulaTextField1.Text="{TBCUSTOM.ID}";
crFormulaTextField2.Text="{TBCUSTOM.NAME}";
this.CrystalReportViewer1.ReportSource = report;
}
结果并未显示任何数据。
测试在窗口上放一个dataGrid控件dataGrid1,设dataGrid1.DataSource=ds,可在dataGrid1里看到正确数据,证明数据已经Fill进了ds。
不知为何在报表中无法显示select出的数据