简体   繁体   English

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

[英]how to set Datatable as datasource in ReportViewer

I was searching in the last question about Datatable as datasource in ReportViewer and i found this as solution 我在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();

but i get this image as result 但我得到这个图像作为结果

在此输入图像描述

what is the problem ?? 问题是什么 ??

It seems you have forgotten to set the report source for your report viewer control. 您似乎忘记为报表查看器控件设置报表源。 You can set the report source using either of this options: 您可以使用以下任一选项设置报告源:

For example, I suppose you have added a report to your project, so you can show it in the report viewer this way: 例如,我假设您已向项目添加了报表,因此您可以通过以下方式在报表查看器中显示它:

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

Also you can simply set the report of the report viewer using designer. 您也可以使用设计器简单地设置报表查看器的报表。 Put a report viewer on your form and click on top-right arrow to open the smart tag window of report viewer, then choose a report from combo box. 将报表查看器放在表单上,​​然后单击右上箭头以打开报表查看器的智能标记窗口,然后从组合框中选择一个报表。

在此输入图像描述

You can add source like below 您可以添加如下所示的来源

LocalReport report = new LocalReport();

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

If i am not wrong, ReportDataSource ctor you are using needs data source in first parameter ie a named data source. 如果我没有错,ReportDataSource是您在第一个参数中使用需求数据源,即命名数据源。 You're not supplying this, you need the DataTable name. 你没有提供这个,你需要DataTable名称。

Update your code to this: 将您的代码更新为:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM