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

时间:2015-12-24 22:31:08

标签: c# winforms datatable reportviewer localreport

我在Datatable中搜索datasource ReportViewer的最后一个问题,我发现这是解决方案

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();

但是我得到了这张图片

enter image description here

问题是什么?

3 个答案:

答案 0 :(得分:4)

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

例如,我认为您已向项目添加了报告,因此您可以通过以下方式在报告查看器中显示该报告:

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

答案 1 :(得分:0)

您可以添加以下内容

LocalReport report = new LocalReport();

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

答案 2 :(得分:0)

如果我没错,ReportDataSource你在第一个参数中使用需求数据源,即命名数据源。你没有提供这个,你需要DataTable名称。

将您的代码更新为:

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