我使用报表查看器创建了一个 Windows 窗体,显示来自 SQL 数据库的数据。到目前为止我做了什么: 1. 我在导入到我的程序的 SQL 服务器上创建了存储过程(实体框架用作对象映射器)。2. 我创建了带有参数的报表(.rdlc),该参数与存储过程中的参数相匹配。3. 最后,我创建了报表查看器并选择了已经创建的报表。
存储过程中有一个参数,如果我使用文本框传递值,一切正常。但是,如果我尝试使用组合框使用“数据绑定项目”传递值,则报告查看器什么也不会显示。这是一个有效的代码示例(通过文本框传递值):
private void btnFind_Click(object sender, EventArgs e)
{
GetCustomer_ResultBindingSource.DataSource = db.GetCustomer(txtProductName.Text).ToList();
ReportParameter[] rParam = new ReportParameter[]
{
new ReportParameter("ProductName",txtProductName.Text)
};
reportViewer1.LocalReport.SetParameters(rParam);
this.reportViewer1.RefreshReport();
}
使用组合框而不是在报表查看器中不显示数据的文本框的解决方案是:
GetCustomer_ResultBindingSource.DataSource = db.GetCustomer(cboProductName.SelectedItem.ToString()).ToList();
ReportParameter[] rParam = new ReportParameter[]
{
new ReportParameter("ProductName",cboProductName.SelectedItem.ToString())
};
reportViewer1.LocalReport.SetParameters(rParam);
this.reportViewer1.RefreshReport();
数据源已加载:
private void Form1_Load(object sender, EventArgs e)
{
ProductBindingSource.DataSource = db.Products.ToList();
}
数据源、显示成员和值成员在组合框的选项中定义(用于填充组合框)。
对第二种解决方案有帮助吗?
非常感谢!