如何将数据表加载为 ReportDataSource?
我想做类似的事情:
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();
如何将数据表加载为 ReportDataSource?
当前代码产生: “无法将类型‘System.Data.DataTable’隐式转换为‘Microsoft.Reporting.WinForms.ReportDataSource’”
I am trying to do something like:
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();
How can I load datatable as ReportDataSource?
The current code produces:
"Cannot implicitly convert type 'System.Data.DataTable' to 'Microsoft.Reporting.WinForms.ReportDataSource' "
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您没有正确初始化 ReportDataSouce。尝试一下:
此外,您可能需要更改 ReportDataSource 构造函数的第一个参数,以设置报表所需的数据源的名称。
You are not initializing the ReportDataSouce correctly. Give this a try:
Also, you might need to alter the first parameter to the ReportDataSource constructor to set the name of the datasource that your report is expecting.
我相信上面 madisonw 的答案是正确的,但 Luke 关于使用 .rdlc 报告文件中指定的数据集名称作为 Datasource.Name 属性的评论可能需要强调。对我来说,这是导致我的应用程序无法运行的主要问题。可以通过使用“打开方式...”命令将 .rdlc 文件作为 XML 文件打开来找到它。我认为默认值只是“DataSet1”:
我犯的另一个错误是没有将 .rdlc 文件包含在调试(或发布)文件夹中。通过右键单击“解决方案资源管理器”中的 .rdlc 文件,然后单击“属性”,然后将“复制到输出目录”设置为“始终复制”,可以修复此问题。
一旦这两部分得到纠正,我的程序就可以在控制台应用程序中使用 ReportViewer 来生成没有界面的 PDF 文件。
I believe that madisonw's answer above is correct, but Luke's comment about using the DataSet name as named in the .rdlc report file, for the Datasource.Name property, perhaps needs to be emphasized. For me this was the main bugaboo that kept my app from working. It can be found by opening the .rdlc file as an XML file by using the "Open with..." command. The default I think is simply "DataSet1":
One other mistake I made was not including the .rdlc file in the Debug (or Release) folder. This was fixed by right-clicking on the .rdlc file in Solution Explorer, then Properties, then setting "Copy to Output Directory" to "Copy Always".
Once these two parts were corrected my program worked to use ReportViewer within a console app to generate a PDF file with no interface.
文件的 DataSources 块如下所示:
想象一下RDLC 数据集>
<数据集名称=“DataSet1_Lot”>
那么相关代码应该是:
string name = "DataSet1_Lot"; // 这必须存在于 RDLC 文件中
DataTable dt = new DataTable();
Microsoft.Reporting.WinForms.ReportDataSource rprtDTSource = new Microsoft.Reporting.WinForms.ReportDataSource(name, dt);
Imagine the DataSources block of your RDLC file is like below:
< DataSets >
< DataSet Name="DataSet1_Lot" >
then the relative code should be:
string name = "DataSet1_Lot"; // this must exist in the RDLC file
DataTable dt = new DataTable();
Microsoft.Reporting.WinForms.ReportDataSource rprtDTSource = new Microsoft.Reporting.WinForms.ReportDataSource(name, dt);