如何在ReportViewer中将Datatable设置为数据源
我在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();
但我得到这个图像的结果
问题是什么 ??
您似乎忘记为报表查看器控件设置报表源。 您可以使用以下任一选项设置报告源:
-
LocalReport.ReportEmbeddedResource
:嵌入报表的资源的名称。 -
LocalReport.ReportPath
:本地报告的文件系统路径。 -
LocalReport.LoadReportDefinition(Stream)
:加载报表定义以便使用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();
您还可以使用设计器简单地设置报表查看器的报表。 将报表查看器放在表单上,然后单击右上箭头以打开报表查看器的智能标记窗口,然后从combobox中选择一个报表。
如果我没有错,ReportDataSource ctor您在第一个参数中使用需求数据源,即命名数据源。 你没有提供这个,你需要DataTable名称。
将您的代码更新为:
DataTable dt = new DataTable(); dt.TableName = "myDataTable"; //Fill Datatable ReportDataSource source = new ReportDataSource("myDataTable", dt);
您可以添加如下所示的来源
LocalReport report = new LocalReport(); string startupPath = Environment.CurrentDirectory; report.ReportPath = startupPath + @"\RDCLTemplate.rdlc"; report.Refresh();