WPF RDLC 报告并传递参数
WPF RDLC Report and pass parameters
当我 运行 我的应用显示 ReportViewer 时遇到了一些问题。这是我的 C# 代码:
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 报告?
创建一个表单并在上面放一个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();
- 请仔细阅读评论
- 查看您的报告,看看它使用的数据集的名称是什么
- 查看您的报表,查看报表中参数的名称,并在传递报表参数时使用它们。
- 在您的代码中而不是在报告中使用参数
当我 运行 我的应用显示 ReportViewer 时遇到了一些问题。这是我的 C# 代码:
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 报告?
创建一个表单并在上面放一个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();
- 请仔细阅读评论
- 查看您的报告,看看它使用的数据集的名称是什么
- 查看您的报表,查看报表中参数的名称,并在传递报表参数时使用它们。
- 在您的代码中而不是在报告中使用参数