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

ala*_*deh 3 c# datatable reportviewer localreport winforms

我在搜索有关Datatableas 的最后一个问题datasourceReportViewer,发现这是解决方案

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();
Run Code Online (Sandbox Code Playgroud)

但是我得到这个图像

在此处输入图片说明

问题是什么 ??

Rez*_*aei 5

您似乎忘记了为报表查看器控件设置报表源。您可以使用以下任一选项来设置报告来源:

例如,假设您已将报告添加到项目中,因此可以通过以下方式在报告查看器中显示它:

var reportDataSource1 = new ReportDataSource("NameOfReportDataSet", YourDataTable);
this.reportViewer1.LocalReport.DataSources.Add(reportDataSource1);
this.reportViewer1.LocalReport.ReportEmbeddedResource = "Namespace.ReportName.rdlc";
this.reportViewer1.RefreshReport();
Run Code Online (Sandbox Code Playgroud)

您也可以使用设计器简单地设置报告查看器的报告。将报表查看器放在表单上,​​然后单击右上角的箭头以打开报表查看器的智能标记窗口,然后从组合框中选择一个报表。

在此处输入图片说明

  • 干得好,您如何在报表中显示数据库数据。您可以用相同的方式显示自定义数据表的数据。还应注意,自定义数据表架构应类似于用于创建报告的表。 (2认同)