在C#参数传递给水晶报表 [英] Passing parameters to crystal reports in C#
问题描述
我一直试图得到这个工作了一段时间,和所有我见过的示例代码都不太做我在做什么。
I've been trying to get this to work for a while, and all the example code I've seen aren't quite doing what I'm doing.
我有一个程序,返回我传递一个数据表,报表的PDF文件。这工作得很好,但我想传递一些其他参数(日期范围的表,统计数据等),我只是无法得到它的工作。我的代码基本上看起来像这样
I have a program that returns a pdf of a report that I pass a data table to. This works fine, except I would like to pass it a couple of other parameters (the date range of the table, stats etc) and I just can't get it to work. My code basically looks like this.
ReportDocument myDataReport = new CrystalDecisions.CrystalReports.Engine.ReportDocument();
myDataReport.Load(@"C:\Layouts\Report.rpt");
ParameterField myParam = new ParameterField();
ParameterDiscreteValue myDiscreteValue = new ParameterDiscreteValue();
myParam.ParameterFieldName = "MyParameter";
myDiscreteValue.Value = "Hello";
myParam.CurrentValues.Add(myDiscreteValue);
myDataReport.ParameterFields.Add(myParam);
myDataReport.SetDataSource(myDataTable);
Stream returnData = myDataReport.ExportToStream(PortableDocFormat);
myDataReport.Close();
return returnData;
我添加参数字段在晶体中的RPT文件中,做我必须要改变的东西在c#XSD文件,还是我失去了完全不同的东西?
I have added the parameter field in the rpt document in crystal, do I have to change anything in the xsd file in c#, or am I missing something completely different?
非常感谢,安迪。
推荐答案
所有这些参数代码可以使用...
All that parameter code can be replaced with...
// Set datasource first
myDataReport.SetDataSource(...)
// Assign Paramters after set datasource
myDataReport.SetParameterValue("MyParameter", "Hello");
我不记得,如果设定DataSource和参数时的顺序很重要。也许首先尝试设置数据源。该XSD /数据源没有任何关系晶体参数。
I can't remember if the order matters when setting the datasource and parameters. Maybe try setting the datasource first. The xsd/datasource has no relation to crystal parameters.
UPDATE1
SetParameterValue AFTER数据源asignation
或您将收到错误缺少参数值。
SetParameterValue AFTER the datasource asignation or you will recieve error "Missing parameter values."
这篇关于在C#参数传递给水晶报表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!