如何将数据表加载为ReportDataSource?

ehm*_*mad 4 c# reporting reportviewer

我想做的事情如下:

this.reportViewer.LocalReport.DataSources.Clear();
DataTable dt = new DataTable();
dt = this.inputValuesTableAdapter.GetData();    
Microsoft.Reporting.WinForms.ReportDataSource rprtDTSource = new Microsoft.Reporting.WinForms.ReportDataSource();

rprtDTSource = dt; // this line generates exception   

//this.reportViewer.LocalReport.DataSources.Add(rprtDTSource);
this.reportViewer.RefreshReport();
Run Code Online (Sandbox Code Playgroud)

如何将数据表加载为ReportDataSource?

当前代码生成: "无法将类型'System.Data.DataTable'隐式转换为'Microsoft.Reporting.WinForms.ReportDataSource'"

mad*_*onw 10

您没有正确初始化ReportDataSouce.尝试一下:

this.reportViewer.LocalReport.DataSources.Clear(); 
DataTable dt = new DataTable(); 
dt = this.inputValuesTableAdapter.GetData();     

Microsoft.Reporting.WinForms.ReportDataSource rprtDTSource = new Microsoft.Reporting.WinForms.ReportDataSource(dt.TableName, dt); 

this.reportViewer.LocalReport.DataSources.Add(rprtDTSource); 
this.reportViewer.RefreshReport(); 
Run Code Online (Sandbox Code Playgroud)

此外,您可能需要将第一个参数更改为ReportDataSource构造函数,以设置报表所期望的数据源的名称.

  • 正如我在第一篇文章末尾提到的那样,您需要将ReportDataSource构造函数的第一个参数设置为报表所查找的数据源的名称.该名称将出现在您现在收到的错误消息中.所以用包含该名称的字符串替换dt.TableName. (2认同)