Reading CrystalReport's Field Value Programmatically
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 fa开发者_如何学Gor:
// 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.
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.
精彩评论