为本地报表设置数据源 - .NET &报告查看器 [英] Setting the datasource for a Local Report - .NET & Report Viewer
问题描述
我创建了一个自定义控件(带有报表查看器的 Windows 窗体).我有以下代码来加载本地报告:
I have created a custom control (a windows form with a report viewer). I have the following code to load a local report:
包含在 CustomReportViewer 类中
//Load local report
this.reportViewer1.ProcessingMode = ProcessingMode.Local;
//enable loading of external images
this.reportViewer1.LocalReport.EnableExternalImages = true;
//pass the report to the viewer
using (FileStream stream = new FileStream(filename, FileMode.Open))
{
this.reportViewer1.LocalReport.LoadReportDefinition(stream);
}
我称之为:
CustomReportViewer reportViewer = new CustomReportViewer();
这很好用,并且会出现一个包含报表查看器控件的 Windows 窗体但是我收到以下消息:
This works fine and a windows form appears containing the report viewer control but I get the following message:
A data source instance has not been supplied for the data source "ReportData"
我不完全确定如何设置数据源?我需要的数据存储在远程数据库中...我需要做什么来建立这个连接?
I'm not entirely sure how to set up the data source? The data I require is stored in a remote database...what do I have to do to set this connection up?
推荐答案
您需要创建一个 ReportDataSource,并设置它的 Value
属性 - 例如DataTable
和 IEnumerable
是 支持的来源
You need to create a ReportDataSource, and set its Value
property - e.g. DataTable
and IEnumerable
s are supported sources
举个例子,假设存在一个方法来返回一个 DataSet,有一个 DataTable
匹配您的报告所需的列:
As an example, and assuming that a method exists to return a DataSet, with a single DataTable
matching the columns needed by your report:
DataSet ds = SomeMethodToRetrieveDataSet(); // e.g. via DataAdapter
// If your report needs parameters, they need to be set ...
ReportParameter[] parameters = new ReportParameter[...];
ReportDataSource reportDataSource = new ReportDataSource();
// Must match the DataSource in the RDLC
reportDataSource.Name = "ReportData";
reportDataSource.Value = ds.Tables[0];
// Add any parameters to the collection
reportViewer1.LocalReport.SetParameters(parameters);
reportViewer1.LocalReport.DataSources.Add(reportDataSource);
reportViewer1.DataBind();
请注意,将 RDLC 嵌入到程序集中通常更容易,而不必保留单独的 RDLC 文件.通过将 RDLC 上的 Build Action
选择为 Embedded Resource
来执行此操作,然后您可以设置 ReportEmbeddedResource
属性:
Note that it is often easier to just embed the RDLC into your assembly, rather than having to retain separate RDLC files. Do this by selecting the Build Action
on the RDLC as Embedded Resource
, and then you can set the ReportEmbeddedResource
property:
reportViewer1.LocalReport.ReportEmbeddedResource =
"MyOrganisation.MyAssembly.NameSpace.MyReportName.rdlc";
请注意,资源字符串必须包含资源的完全限定名称(包括程序集).
Note that the resource string must include the fully qualified name of the resource (including Assembly).
这篇关于为本地报表设置数据源 - .NET &报告查看器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!