无法从DataSet填充RDLC报表:ReportDataSource构造函数报错求助
解决RDLC报表ReportDataSource构造函数重载错误
问题场景
手动创建包含DataTable的DataSet,通过VB.NET代码为DataTable列赋值后,尝试绑定到RDLC报表时出错。RDLC文件配置:
- 数据源名称:
companyReportBudget - 数据集名称:
Report(基于companyReportBudget,可用数据集为reportBudget)
使用的填充代码如下:
Dim rptBudget As New companyReportBudget.reportBudgetDataTable rptBudget.Clear() For Each rowBudget As RequestGetBudget In rowsBudget Dim reportRow As DataRow = rptBudget.NewRow() reportRow("num") = rowBudget.number reportRow("code") = rowBudget.code rptBudget.Rows.Add(reportRow) Next Dim rds As New ReportDataSource("Report", rptBudget) rptStampaBudget.RefreshReport()
错误信息
Overload resolution failed because no accessible 'New' accepts this number of type arguments.
系统环境
- OS: Windows 10 Pro (22H2)
- Visual Studio: Community 2017 Version 15.9.55
- NuGet包:
- Microsoft.ReportViewer.WinForms.v12 - 12.0.0.1
- Microsoft.ReportingServices.ReportViewerControl.Winforms - 150.1586.0
解决方法
1. 解决命名空间与构造函数冲突
你同时安装了v12和v15两个版本的ReportViewer控件,不同版本的ReportDataSource构造函数差异导致报错:
- 旧版(
Microsoft.Reporting.WinForms.ReportDataSource)支持双参数构造函数New(name As String, dataSource As Object) - 新版(
Microsoft.ReportingServices.ReportViewerControl.WinForms.ReportDataSource)无此重载,需通过属性赋值
方案A:指定使用旧版控件
修改ReportDataSource声明,明确使用旧版命名空间:
Dim rds As New Microsoft.Reporting.WinForms.ReportDataSource("Report", rptBudget)
方案B:适配新版控件构造函数
若要使用v15版本,调整代码为属性赋值方式:
Dim rds As New Microsoft.ReportingServices.ReportViewerControl.WinForms.ReportDataSource() rds.Name = "Report" rds.Value = rptBudget
2. 清理冲突的NuGet包
同时存在两个版本的ReportViewer会引发命名空间混淆,建议只保留一个版本:
- 若项目依赖旧版RDLC,卸载
Microsoft.ReportingServices.ReportViewerControl.Winforms,保留Microsoft.ReportViewer.WinForms.v12 - 若升级到新版,卸载旧版包,并确认RDLC文件适配新版控件格式
3. 验证数据源名称匹配
确保代码中"Report"与RDLC文件中数据集的名称完全一致(RDLC数据集名称区分大小写)
内容的提问来源于stack exchange,提问作者Swim89
相关产品推荐
相关产品推荐

