ala*_*deh 3 c# datatable reportviewer localreport winforms
我在搜索有关Datatableas 的最后一个问题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();
Run Code Online (Sandbox Code Playgroud)
但是我得到这个图像
问题是什么 ??
您似乎忘记了为报表查看器控件设置报表源。您可以使用以下任一选项来设置报告来源:
LocalReport.ReportEmbeddedResource :报表嵌入资源的名称。 LocalReport.ReportPath :本地报告的文件系统路径。LocalReport.LoadReportDefinition(Stream):加载报告定义以使用流进行处理。LocalReport.LoadReportDefinition(TextReader) 使用TextReader从本地文件系统加载报告定义。例如,假设您已将报告添加到项目中,因此可以通过以下方式在报告查看器中显示它:
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)
您也可以使用设计器简单地设置报告查看器的报告。将报表查看器放在表单上,然后单击右上角的箭头以打开报表查看器的智能标记窗口,然后从组合框中选择一个报表。