使用绑定DataTable的动态DataSet时ReportViewer显示空白无文字问题排查
问题根因与修复方案
核心错误点
- 报表资源加载顺序错误:你当前先添加数据源再指定RDLC嵌入资源,加载报表资源的操作会清空之前配置的数据源绑定关系,导致字段无法映射,所以只显示行数没有文字内容
- 刷新调用逻辑混乱,无效刷新操作过多
- 潜在问题:RDLC设计时的数据集名称、绑定字段名和代码中传递的不一致也会触发该问题
修复后代码
Dim f4 As New Form4 Dim ds2 As New NewDS ' NewDS=自定义数据集类型 Dim xtbl As DataTable = CType(DgvEmp.DataSource, DataTable) ds2.Reset() ds2.DataSetName = "newTable" ds2.Tables.Add(xtbl.Copy()) ds2.Tables(0).TableName = "DT" Dim rds As New ReportDataSource(ds2.Tables(0).TableName, ds2.Tables(0)) ' 正确顺序:先设置处理模式、指定报表资源,再操作数据源 f4.ReportViewer1.ProcessingMode = ProcessingMode.Local f4.ReportViewer1.LocalReport.ReportEmbeddedResource = "EmployeeBouncer.Report2.rdlc" f4.ReportViewer1.LocalReport.DataSources.Clear() f4.ReportViewer1.LocalReport.DataSources.Add(rds) ' 最后只需要调用RefreshReport即可完成渲染 f4.ReportViewer1.RefreshReport() f4.Show()
额外检查项
- 打开Report2.rdlc设计界面,确认报表里定义的数据集名称和代码中
ds2.Tables(0).TableName的值完全一致(区分大小写) - 确认RDLC中绑定的字段名称和xtbl的列名完全一致,没有拼写错误
内容的提问来源于stack exchange,提问作者Abdulla habib
相关产品推荐
相关产品推荐

