问题描述
你能告诉我将参数传递给C#win形式的水晶报表13的步骤是什么吗..
Can you tell me what are the steps to pass parameters to crystal reports 13 in C# win form..
我的代码:
//getting and set dataset to report
string sql = "select * from dbo.Trading_Order";
DataRetriever dr = new DataRetriever();
dr.getValueFromCustomer(sql);
DataTable dtSum = dr.getDataTable();
dsMyReprt k = new dsMyReprt();
k.Tables.Remove("dtMyTable");
dtSum.TableName = "dtMyTable";
k.Tables.Add(dtSum);
CrystalReport1 myDataReport = new CrystalReport1();
//pass parameter
ParameterFields paramFields = new ParameterFields();
// ParameterDiscreteValue paramDiscreteValue = new ParameterDiscreteValue();
ParameterField paramField = new ParameterField();
ParameterDiscreteValue paramDiscreteValue = new ParameterDiscreteValue();
paramField.Name = "@DTotal";
paramDiscreteValue.Value = tot;
paramField.CurrentValues.Add(paramDiscreteValue);
paramFields.Add(paramField);
paramField = new ParameterField();
paramDiscreteValue = new ParameterDiscreteValue();
paramField.Name = "@name";
paramDiscreteValue.Value = name;
paramField.CurrentValues.Add(paramDiscreteValue);
paramFields.Add(paramField);
crystalReportViewer1.ParameterFieldInfo = paramFields;
myDataReport.SetDataSource(k);
crystalReportViewer1.ReportSource = myDataReport;
获取和设置数据集部分正在工作但是传递参数部分不起作用
getting and set dataset part is working but passing parameters part is not working
推荐答案
我为此头疼了好几个星期……我必须准确地说我在 Crystal Reports 设计器中设置了一个 sql 查询.因此,我没有像您那样使用数据表,因此您必须考虑这一点.
I got big headaches with that for weeks... I have to precise that I set a sql query in the Crystal Reports Designer. Thus, I didn't use a dataTable like you did, so you have to consider that.
好吧,@campagnolo_1 建议你:
Well, @campagnolo_1 suggested you :
ReportDocument myDataReport = new ReportDocument();
myDataReport.Load(@"C:ReportsReport.rpt");
myDataReport.SetParameterValue("MyParameter1", "Hello1");
myDataReport.SetParameterValue("MyParameter2", "Hello2");
myDataReport.SetParameterValue("MyParameter3", "Hello3");
这是简短而甜蜜的解决方案.但是,在此之后,您必须确保已在 Crystal Reports 设计器中创建了 String 类型的 MyParameter1
、MyParameter2
和 MyParameter3
.
This is the short and sweet solution. But, following this, you have to make sure you have created MyParameter1
, MyParameter2
and MyParameter3
of type String in the Crystal Reports Designer.
需要指出的是,在使用
SetParameterValue
设置参数之前,您必须先加载报告.
It's important to mention that you have to Load the report before setting your parameters with
SetParameterValue
.
如果你的参数名称是MyParameter1
,那么不要在前面添加@
这样的:
If your parameter's name is MyParameter1
, then don't add a @
in front like this :
myDataReport.SetParameterValue("@MyParameter1", "Hello1");//你的程序会崩溃.
myDataReport.SetParameterValue("@MyParameter1", "Hello1"); // Your program will crash.
如果你得到参数不正确
那么你应该确保你给出的参数值的类型与参数类型完全一样.例如,如果您有一个参数 StartDate
作为 Date 类型,那么请确保您给出的值是 Date 类型并且具有正确的日期格式.
If you got The parameter is incorrect
then you should make sure the type of parameter value you gave is exactly the same as the parameter type. For example, if you have a parameter StartDate
as type Date, then make sure the value you'll give is of type Date and has the right date format.
此外,您还谈到了动态或静态字段.在您的情况下,我认为您手动输入值,那么这是静态字段.
Also, you have talked about dynamic or static field. In your case, I think you enter values manually, then this is static field.
希望对你有所帮助.
这篇关于在 C# 中将参数传递给水晶报表的步骤的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!