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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19930121/
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-10 16:08:52  来源:igfitidea点击:

Steps to Pass parameters to crystal reports in C#

c#visual-studio-2010crystal-reports

提问by NipunR

Can you tell me what are the steps to pass parameters to crystal reports 13 in C# win form..

你能告诉我在C#win形式中将参数传递给水晶报表13的步骤是什么吗?

my code:

我的代码:

        //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

获取和设置数据集部分有效,但传递参数部分无效

回答by campagnolo_1

Why not try it this way and save yourself some coding?

为什么不以这种方式尝试并为自己节省一些编码?

ReportDocument myDataReport = new CrystalDecisions.CrystalReports.Engine.ReportDocument();
myDataReport.Load(@"C:\Reports\Report.rpt");

myDataReport.SetParameterValue("MyParameter1", "Hello1");
myDataReport.SetParameterValue("MyParameter2", "Hello2");
myDataReport.SetParameterValue("MyParameter3", "Hello3");

回答by Gabriel L.

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.

我为此头疼了好几个星期……我必须明确指出,我在 Crystal Reports Designer 中设置了一个 sql 查询。因此,我没有像您那样使用 dataTable,所以您必须考虑这一点。

Well, @campagnolo_1 suggested you :

好吧,@campagnolo_1 建议你:

ReportDocument myDataReport = new ReportDocument();
myDataReport.Load(@"C:\Reports\Report.rpt");

myDataReport.SetParameterValue("MyParameter1", "Hello1");
myDataReport.SetParameterValue("MyParameter2", "Hello2");
myDataReport.SetParameterValue("MyParameter3", "Hello3");

This is the short and sweet solution. But, following this, you have to make sure you have created MyParameter1, MyParameter2and MyParameter3of type String in the Crystal Reports Designer.

这是简短而甜蜜的解决方案。但是,在此之后,您必须确保在 Crystal Reports 设计器中创建了MyParameter1,MyParameter2并且MyParameter3类型为 String 。

  1. It's important to mention that you have to Load the report before setting your parameters with SetParameterValue.

  2. If your parameter's name is MyParameter1, then don't add a @in front like this :

    myDataReport.SetParameterValue("@MyParameter1", "Hello1"); // Your program will crash.

  3. If you got The parameter is incorrectthen 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 StartDateas type Date, then make sure the value you'll give is of type Date and has the right date format.

  1. 值得一提的是,您必须先加载报告,然后才能使用SetParameterValue.

  2. 如果您的参数名称是MyParameter1,则不要@像这样在前面添加 a :

    myDataReport.SetParameterValue("@MyParameter1", "Hello1"); // 你的程序会崩溃。

  3. 如果你得到了,The parameter is incorrect那么你应该确保你给出的参数值的类型与参数类型完全相同。例如,如果您有一个StartDateDate 类型的参数,那么请确保您提供的值属于 Date 类型并且具有正确的日期格式。

Also, you have talked about dynamic or static field. In your case, I think you enter values manually, then this is static field.

此外,您已经谈到了动态或静态字段。在您的情况下,我认为您手动输入值,那么这是静态字段。

Hope this helps you.

希望这对你有帮助。

回答by Kinyanjui Kamau

You probably have figured the solution by now. But this may help. You enter values that are passed as a parameter.

您现在可能已经找到了解决方案。但这可能会有所帮助。您输入作为参数传递的值。

http://www.codeproject.com/Tips/753879/Automatically-Setting-a-Parameter-from-a-Csharp-Va

http://www.codeproject.com/Tips/753879/Automatically-Setting-a-Parameter-from-a-Csharp-Va