我正在报告查看器中构建报告。使用单个数据集,效果很好,但我需要在同一个报告中包含多个数据集。你能告诉我我做错了什么吗(如果可能的话,请提供一些代码)。我找到了关于这个问题的一大堆信息,但所有内容都不是使用相同的编程语言。我正在使用 C#。在 .RDLC 中,我创建了一个数据源和 2 个数据集(DataSet 和 DataSet1)。这是我当前的代码:
private void LoadReport()
{
try
{
MySqlConnection con = new MySqlConnection(conSettings.ToString());
MySqlCommand cmd = new MySqlCommand("packing_slips", con);
MySqlCommand cmd1 = new MySqlCommand("client_info", con);
con.Open();
cmd.Parameters.Add("@project", MySqlDbType.VarChar, 20).Value = project_id_box.Text;
cmd.CommandType = CommandType.StoredProcedure;
cmd1.Parameters.Add("@project", MySqlDbType.VarChar, 20).Value = project_id_box.Text;
cmd1.CommandType = CommandType.StoredProcedure;
MySqlDataAdapter adp = new MySqlDataAdapter(cmd);
MySqlDataAdapter adp1 = new MySqlDataAdapter(cmd1);
DataSet ds = new DataSet();
DataSet ds1 = new DataSet();
adp.Fill(ds);
adp1.Fill(ds1);
reportViewer1.Reset();
this.reportViewer1.LocalReport.DataSources.Clear();
ReportDataSource reportDataSource = new ReportDataSource();
reportDataSource.Value = ds.Tables[0];
reportDataSource.Name = "DataSet";
ReportDataSource reportDataSource1 = new ReportDataSource();
reportDataSource1.Value = ds1.Tables[0];
reportDataSource1.Name = "DataSet1";
this.reportViewer1.LocalReport.DataSources.Add(reportDataSource);
this.reportViewer1.LocalReport.DataSources.Add(reportDataSource1);
this.reportViewer1.LocalReport.ReportPath = "project_report.rdlc";
this.packing_slipsTableAdapter.Fill(this.shopmanagerDataSet.packing_slips);
this.projectsTableAdapter.Fill(this.shopmanagerDataSet.projects);
this.reportViewer1.RefreshReport();
con.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
编译时我没有收到任何编译错误,但报告是空白的。我正在使用存储过程从 MySQL 数据库检索数据。调试时,我看到 ds 和 ds1 已正确填充。谢谢。
最佳答案
我找到了答案。将多个数据集添加到 RDLC 时,您需要在文本框属性中指定信息来自哪个数据集。例如:
=First(Fields!project_number.Value, "DataSet")
这为我解决了这个问题。上面的代码对于其他遇到此问题的人来说很有用。
关于c# - C# 中多个数据集 Winform 项目的 RDLC 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53524602/