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

时间:2009-10-19 14:33:38

标签: c# crystal-reports

我一直试图让它工作一段时间,我见过的所有示例代码都没有完全按照我正在做的事情进行。

我有一个程序返回我传递数据表的报告的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;

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

非常感谢,安迪。

5 个答案:

答案 0 :(得分:25)

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

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

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

<强> UPDATE1

SetParameterValue在数据源yignation之后 或者你会收到错误“缺少参数值。”

答案 1 :(得分:3)

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(); 

答案 2 :(得分:1)

       //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();

答案 3 :(得分:0)

                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();

答案 4 :(得分:0)

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

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