3

我有一个水晶报告,我在其中以编程方式设置数据源和参数。当我只在报告中显示常规数据时,这很有效。但是,当我尝试在报告标题中显示参数值(它是一个日期)时,系统会提示我输入参数。关于造成这种情况的任何想法?我是水晶报告的新手,所以这可能是一个非常简单的问题。

这是我的代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using System.Data.SqlClient;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;

namespace CrystalReportsTestApp
{
    public partial class ReportForm : Form
    {
        ReportDocument report = new ReportDocument();

        public ReportForm()
        {
            InitializeComponent();           
        }

        private void ConfigureCrystalReports()
        {
            string reportPath = "mypath\\CrystalReport1.rpt";
            report.Load(reportPath);
        }

        private void RunReportButton_Click(object sender, EventArgs e)
        {
            DateTime date = dateTimePicker1.Value;

            DataSet reportData = new DataSet();

            SqlConnection conn = null;
            SqlDataAdapter da = null;

            try
            {
                conn = new SqlConnection(connectionString);
                conn.Open();
                da = new SqlDataAdapter();

                SqlCommand cmd = new SqlCommand("cms.GetActiveEntityAccounts", conn);
                cmd.CommandType = CommandType.StoredProcedure;

                cmd.Parameters.Add("@EndDate", SqlDbType.DateTime).Value = date;

                da.SelectCommand = cmd;
                da.Fill(reportData, "DataTable1");

            }
            finally
            {
                if (conn != null)
                {
                    conn.Close();
                }
            }

            report.SetDataSource(reportData);
            report.SetParameterValue("@EndDate", date);

            crystalReportViewer1.ReportSource = report;
            crystalReportViewer1.RefreshReport();         
        }

        private void ReportForm_Load(object sender, EventArgs e)
        {
            ConfigureCrystalReports();
        }
    }
    }
4

2 回答 2

4

问题是最后一行:

crystalReportViewer1.RefreshReport();    

这会导致报表参数刷新,而不仅仅是报表数据。

于 2012-09-05T08:29:20.327 回答
3

您需要将参数传递给 CrystalReportViewer 控件。之后使用下面的代码report.SetDataSource(reportData);

CrystalDecisions.Shared.ParameterField parameterField = new CrystalDecisions.Shared.ParameterField();
parameterField.Name = "@EndDate";
//Create a new Discrete Value
CrystalDecisions.Shared.ParameterDiscreteValue parameterDiscreteValue = new CrystalDecisions.Shared.ParameterDiscreteValue();
parameterDiscreteValue.Value = date;

//Add the value
parameterField.CurrentValues.Add( parameterDiscreteValue );

//Add the parameter field
crystalReportViewer1.ParameterFields.Add( parameterField );
crystalReportViewer1.ReportSource = report;
crystalReportViewer1.RefreshReport();  
于 2012-09-05T12:02:17.900 回答