WPF RDLC报告和传递参数

时间:2015-09-01 19:32:33

标签: c# .net wpf report rdlc

运行应用程序以显示ReportViewer时出现问题。这是我的C#代码: enter image description here

private void Report_Load(object sender, EventArgs e)
            {
                ShowReport();
            }
            private DataTable GetData()
            {

                DateTime dtStart = DateTime.Parse(txtDataS.Text);
                DateTime dtEnd = DateTime.Parse(txtDataE.Text);
                DataTable _dt = new DataTable();
                using (_con = new SqlConnection(_strConexao))
                {
                _cmd = new SqlCommand("SELECT_CADS", _con);
                _cmd.CommandType = CommandType.StoredProcedure;
                _cmd.Parameters.Add("@SDATE", SqlDbType.DateTime).Value = dtStart;
                _cmd.Parameters.Add("@EDATE", SqlDbType.DateTime).Value = dtEnd;
                _adp = new SqlDataAdapter(_cmd);
                _adp.Fill(_dt);

            }
            return _dt;
        }

        private void ShowReport()
        {
            _reportViewer.Reset();
            DataTable _dt = GetData();
            ReportDataSource rds = new ReportDataSource("DataSetRel",_dt);
            _reportViewer.LocalReport.DataSources.Add(rds);
            _reportViewer.LocalReport.ReportEmbeddedResource = "Project.Rel.rdlc";
            _reportViewer.ProcessingMode = ProcessingMode.Local;

            ReportParameter dt1 = new ReportParameter("dtStart", txtDataS.Text);
            ReportParameter dt2 = new ReportParameter("dtEnd", txtDataE.Text);

            _reportViewer.LocalReport.SetParameters(new ReportParameter[] {dt1,dt2});
            _reportViewer.LocalReport.Refresh(); 
        }

当我运行此应用程序并输入初始日期和最终日期并单击显示按钮时没有发生在我的rdlc报告中注意我有两个参数dtStart和dtEnd我用它作为文本框中的表达式。什么是问题?为什么我不能将此参数传递给rdlc报告?

1 个答案:

答案 0 :(得分:1)

创建一个表单并在其上放置一个ReportViewr,然后在表单加载事件中编写这样的代码:

var reportBindingSource = new System.Windows.Forms.BindingSource();

var reportDataSource = new Microsoft.Reporting.WinForms.ReportDataSource();
//Name of dataset in your rdlc report
reportDataSource.Name = "DataSet1";
reportDataSource.Value = reportBindingSource;

this.reportViewer1.LocalReport.DataSources.Add(reportDataSource);
this.reportViewer1.LocalReport.ReportEmbeddedResource = "StackSamplesCS.Data.Report1.rdlc";

//Set parameters
//These are repot parameters, so use the names that you gave them in report.
this.reportViewer1.LocalReport.SetParameters(new Microsoft.Reporting.WinForms.ReportParameter("StartDate", this.DatePicker1.SelectedDate.ToString()));
this.reportViewer1.LocalReport.SetParameters(new Microsoft.Reporting.WinForms.ReportParameter("EndDate", this.DatePicker1.SelectedDate.ToString()));

//put your connection string
//example: @"data source=(localdb)\v11.0;initial catalog=YourDatabase;integrated security=True;"
//example @".\sqlexpress;;initial catalog=YourDatabase;integrated security=True;"
var connection = W"Your Connection String" ;

//your command
//example: "SELECT * FROM YourTable WHERE StartDate>@StartDate AND EndDate<@EndDate"
var command = "Your Command";

var tableAdapter = new System.Data.SqlClient.SqlDataAdapter(command, connection);

//Set Sql Parameters
tableAdapter.SelectCommand.Parameters.Add(new System.Data.SqlClient.SqlParameter("@StartDate", this.DatePicker1.SelectedDate));
tableAdapter.SelectCommand.Parameters.Add(new System.Data.SqlClient.SqlParameter("@EndDate", DatePicker2.SelectedDate));
var dataTable= new DataTable();
//Get data
tableAdapter.Fill(dataTable);

reportBindingSource.DataSource = dataTable;

this.reportViewer1.RefreshReport();
  • 请仔细阅读评论
  • 查看您的报告,看看它使用的DataSet的名称是什么
  • 查看您的报告,查看报告中参数的名称,并在传递报告参数时使用它们。
  • 在代码中使用不在报告
  • 中的参数