You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

RDLC报表无数据显示:基于DataTable生成报表的问题排查

RDLC报表绑定内存DataTable无数据的解决办法

问题根源分析

你的代码存在两处关键错误,导致DataTable的数据无法正确传递到报表:

  1. DataSet添加表的方式错误:你用dataSet1.Tables.Add(dTStudentDetails.TableName)只是添加了一个同名空表,并没有把已经填充好数据的dTStudentDetails加入DataSet。
  2. ReportDataSource参数错误:构造数据源时,你传递的是dataSet1.Tables(整个表集合),而报表需要的是具体的单个数据表。

修正后的关键代码

// 替换原DataSet和ReportDataSource相关代码
DataSet1 dataSet1 = new DataSet1();
DataSet2 dataSet2 = new DataSet2();

// 直接将填充好数据的DataTable添加到DataSet,而非仅添加表名
dataSet1.Tables.Add(dTStudentDetails.Copy()); // 用Copy避免原表被关联影响
dataSet2.Tables.Add(dTPayDetails.Copy());

// 构造ReportDataSource时,第二个参数指定DataSet中的具体表
// 注意:第一个参数要和RDLC报表中设置的数据源名称完全一致
ReportDataSource rds1 = new ReportDataSource("DataSet1", dataSet1.Tables["dTStudentDetails"]);
ReportDataSource rds2 = new ReportDataSource("DataSet2", dataSet2.Tables["dTPayDetails"]);

this.reportViewer1.LocalReport.DataSources.Clear();
this.reportViewer1.LocalReport.DataSources.Add(rds1);
this.reportViewer1.LocalReport.DataSources.Add(rds2);

this.reportViewer1.LocalReport.Refresh();
this.reportViewer1.RefreshReport();

额外优化方案

如果不想依赖DataSet,也可以直接将DataTable绑定到ReportDataSource,简化代码:

// 无需创建DataSet,直接用内存中的DataTable作为数据源
ReportDataSource rds1 = new ReportDataSource("DataSet1", dTStudentDetails);
ReportDataSource rds2 = new ReportDataSource("DataSet2", dTPayDetails);

this.reportViewer1.LocalReport.DataSources.Clear();
this.reportViewer1.LocalReport.DataSources.Add(rds1);
this.reportViewer1.LocalReport.DataSources.Add(rds2);

this.reportViewer1.LocalReport.Refresh();
this.reportViewer1.RefreshReport();

验证要点

  • 确认RDLC报表中定义的数据集名称和代码中ReportDataSource的第一个参数完全匹配(包括大小写)。
  • 调试时检查dTStudentDetails.Rows.Count和dTPayDetails.Rows.Count是否大于0,确保数据已正确填充。

内容的提问来源于stack exchange,提问作者Prashant soni

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.19 12:10:55