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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1589021/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 19:06:35  来源:igfitidea点击:

Passing parameters to crystal reports in C#

c#crystal-reports

提问by

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.

我一直在努力让它工作一段时间,我看到的所有示例代码都没有完全做我正在做的事情。

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.

我有一个程序可以返回我将数据表传递给的报告的 pdf。这工作正常,除了我想向它传递几个其他参数(表格的日期范围、统计信息等),但我无法让它工作。我的代码基本上是这样的。

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;

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?

我已经在水晶的rpt文档中添加了参数字段,我是否必须在c#中更改xsd文件中的任何内容,或者我是否遗漏了完全不同的内容?

Many thanks, Andy.

非常感谢,安迪。

采纳答案by dotjoe

All that parameter code can be replaced with...

所有这些参数代码都可以替换为...

// Set datasource first
myDataReport.SetDataSource(...)
// Assign Paramters after set datasource
myDataReport.SetParameterValue("MyParameter", "Hello");

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.

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

UPDATE1

更新1

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

在数据源分配之后设置参数值,否则您将收到错误“缺少参数值”。

回答by mitsu

ReportDocument cryRpt = new ReportDocument();

TableLogOnInfos crtableLogoninfos = new TableLogOnInfos();
TableLogOnInfo crtableLogoninfo = new TableLogOnInfo();
ConnectionInfo crConnectionInfo = new ConnectionInfo();
Tables CrTables;

string path = "C:/reportpath/report.rpt";
cryRpt.Load(path);

cryRpt.SetParameterValue("MyDate2", str2);
cryRpt.SetParameterValue("MyDate", str1);

crConnectionInfo.ServerName = "server";
crConnectionInfo.DatabaseName = "DataBase";
crConnectionInfo.UserID = "user";
crConnectionInfo.Password = "password";

CrTables = cryRpt.Database.Tables;
foreach (CrystalDecisions.CrystalReports.Engine.Table CrTable in CrTables)
{
    crtableLogoninfo = CrTable.LogOnInfo;
    crtableLogoninfo.ConnectionInfo = crConnectionInfo;
    CrTable.ApplyLogOnInfo(crtableLogoninfo);
}

crystalReportViewer1.ReportSource = cryRpt;
crystalReportViewer1.Refresh(); 

回答by P.Nagasiva

       //create object of crystal report.
        CrystalReport1 objRpt = new CrystalReport1();
        objRpt.SetDataSource(ds);
        ParameterFields pfield = new ParameterFields();
        ParameterField ptitle = new ParameterField();
        ParameterDiscreteValue pvalue = new ParameterDiscreteValue();
        ptitle.ParameterFieldName = "date";
        pvalue.Value = txtcolor.Text;
        ptitle.CurrentValues.Add(pvalue);
        pfield.Add(ptitle);
        crystalReportViewer1.ParameterFieldInfo = pfield;
        crystalReportViewer1.ReportSource = objRpt;
        crystalReportViewer1.Refresh();

回答by sanuj dananjaya

                rptTeacherTimeTable ttReport = new rptTeacherTimeTable();
                DataTable dt = new DataTable();
                dt = ObjclsT_TimeTable.GetTimeTableByClass(ClassID);

                objReport = ttReport;
                objReport.SetDataSource(dt);
                objReport.SetParameterValue("RTitle", "Class Time Table");
                objReport.SetParameterValue("STitle", "Teacher Time Table");
                reportViewer.crystalReportViewer1.ReportSource = objReport;
                reportViewer.crystalReportViewer1.Show();
                reportViewer.Show();

回答by Tahir Shahzad

just do one thing and your code will run automatically set the data source first before adding parameters...

只需做一件事,您的代码就会自动运行,在添加参数之前先设置数据源...

 crp rpt = new crp();
 rpt.SetDataSource(dt);
 rpt.SetParameterValue("p",p.ToString());