在 C# 中将参数传递给水晶报表

Passing parameters to crystal reports in C#(在 C# 中将参数传递给水晶报表)

本文介绍了在 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:LayoutsReport.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");

我不记得在设置数据源和参数时顺序是否重要.也许先尝试设置数据源.xsd/datasource 与晶体参数无关.

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.

更新1

在数据源分配之后设置参数值否则您将收到错误缺少参数值".

SetParameterValue AFTER the datasource asignation or you will recieve error "Missing parameter values."

这篇关于在 C# 中将参数传递给水晶报表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:在 C# 中将参数传递给水晶报表

基础教程推荐