Reading CrystalReport's Field Value Programmatically

六眼飞鱼酱① 提交于 2020-01-15 12:38:10

问题


I'm using C#.NET to programmatically load a Crystal Report document (which gets its data from a database) and then export it as a PDF.

I'm stuck trying to read a fields (abc123) value from within the report (I want to use it in the filename). I don't mind where this field is in the crystal report; and it will not be repeated, but it needs to be a field in the report.

My code so far:

// Setup
ReportDocument reportTemplate = new ReportDocument();
reportTemplate.Load(textBox1.Text);
reportTemplate.Refresh();
reportTemplate.ReadRecords();

// Load the current reports name from the report summary information
String ReportName = reportTemplate.SummaryInfo.ReportTitle.ToString();

// Load the current reports "abc123" field from the report
// ????

// Export the report as PDF
reportTemplate.Export();

I've tried:

// Load the current reports "abc123" field from the report
String abcField = ((TextObject)rpt.Section2.ReportObjects["abc123"]).Text
String abcField = reportTemplate.ReportDefinition.ReportObjects["abc123"].ToString();
String abcField = reportTemplate.Rows.ReportDocument.ReportDefinition.ReportObjects["abc123"];

Without luck. Can anyone give some pointers? I can preview the report using crystalReportView1 object and the field is populated there.


回答1:


Unfortunately you will not be able to pull a calculated field value from code behind. The calculated field value is not available until after the report engine takes over and does the rendering. You can access/modify formula definitions before the report is rendered but have no access to the actual value calculated from code behind. Even if you add a blank subreport and add a parameter to it from the main report for the field value you are trying to access it will not have a value before the reports engine renders it. You should definitely look for a way to pull it from the data you are binding the report with.



来源:https://stackoverflow.com/questions/7508485/reading-crystalreports-field-value-programmatically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!