how to set Datatable as datasource in ReportViewer

≯℡__Kan透↙ 提交于 2019-11-28 01:46:57

问题


I was searching in the last question about Datatable as datasource in ReportViewer and i found this as solution

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 ??


回答1:


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:

  • LocalReport.ReportEmbeddedResource : The name of the report-embedded resource.
  • LocalReport.ReportPath : The file system path of the local report.
  • LocalReport.LoadReportDefinition(Stream): Loads a report definition for processing using a Stream.
  • LocalReport.LoadReportDefinition(TextReader) Loads a report definition from the local file system using a TextReader.

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.




回答2:


If i am not wrong, ReportDataSource ctor you are using needs data source in first parameter i.e. a named data source. You're not supplying this, you need the DataTable name.

Update your code to this:

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



回答3:


You can add source like below

LocalReport report = new LocalReport();

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


来源:https://stackoverflow.com/questions/34458434/how-to-set-datatable-as-datasource-in-reportviewer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!