如何在ReportViewer中将Datatable设置为数据源

3

我在上一个问题中搜索有关在ReportViewer中使用Datatable作为datasource的内容,找到了这个作为解决方案:

DataTable table = new DataTable();
table.Columns.Add("value", typeof(string));
table.Columns.Add("price", typeof(string));
table.Columns.Add("quantity", typeof(string));

table.Rows.Add("test1","10","20");
table.Rows.Add("test2", "10", "20");

reportViewer1.LocalReport.DataSources.Clear();

ReportDataSource rprtDTSource = new ReportDataSource("TITLE",table);

reportViewer1.LocalReport.DataSources.Add(rprtDTSource);
reportViewer1.RefreshReport();

但我得到了这张图片作为结果

在此输入图片描述

问题出在哪里?


你在哪里为报表查看器设置报表?我想你有一个嵌入式资源的报表,然后你可以设置报表源,就像这样:this.reportViewer1.LocalReport.ReportEmbeddedResource = "Namespace.ReportName.rdlc"; - Reza Aghaei
我只有一个数据表和报表查看器,我该如何在报表查看器中显示数据?@RezaAghaei - ala sawa'deh
3个回答

5
似乎您忘记为报表查看器控件设置报表来源。您可以使用以下任一选项设置报表来源: 例如,假设您已将报表添加到项目中,您可以按照以下方式在报表查看器中显示它:
var reportDataSource1 = new ReportDataSource("NameOfReportDataSet", YourDataTable);
this.reportViewer1.LocalReport.DataSources.Add(reportDataSource1);
this.reportViewer1.LocalReport.ReportEmbeddedResource = "Namespace.ReportName.rdlc";
this.reportViewer1.RefreshReport();

您可以直接使用设计器设置报表查看器的报表。将报表查看器放置在您的表单上并点击右上角的箭头以打开报表查看器的智能标记窗口,然后从组合框中选择报表。
图片操作请参考以下网址:enter image description here

是的,我执行了这个操作:向项目添加一个新的空报表,并将其选择为报表查看器的资源。 - ala sawa'deh
1
创建一个空报告是没有用的。你应该向报告添加数据源并设计报告。然后将你的数据传递给报告。你可以使用报告向导。 - Reza Aghaei
2
干得好,你如何在报告中展示数据库的数据。你可以用同样的方式展示自定义数据表的数据。另外,你需要注意,自定义数据表架构应该与你用于创建报告的表相同。 - Reza Aghaei
1
此外,您应该知道,如果您有一个名为ProductDataTable的数据表,您不需要创建一个自定义表并将其传递给报告,您可以直接使用该表。特别是如果您已经创建了包含数据表的数据集,并且正在使用绑定源作为报告的数据源,因此您可以简单地向yourSataSet.YourTable.Rows的实例中添加一些行,然后调用RefreshReport方法即可。 - Reza Aghaei
@RodrigoRodriguez 不用谢,很高兴听到对你有帮助 :) - Reza Aghaei
显示剩余5条评论

0

你可以像以下这样添加源代码

LocalReport report = new LocalReport();

string startupPath = Environment.CurrentDirectory;
report.ReportPath = startupPath + @"\RDCLTemplate.rdlc";
report.Refresh();

0

如果我没记错的话,你正在使用的ReportDataSource构造函数需要在第一个参数中提供数据源,即命名数据源。你没有提供这个,你需要提供DataTable名称。

请将您的代码更新为以下内容:

DataTable dt = new DataTable();
dt.TableName = "myDataTable";
//Fill Datatable
ReportDataSource source = new ReportDataSource("myDataTable", dt);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接