设置本地报表数据源 - .NET和放大器;报表查看器 [英] Setting the datasource for a Local Report - .NET & Report Viewer
问题描述
我创建了一个自定义的控件(Windows窗体与报表查看器)。我有以下的code加载本地报表:
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?
推荐答案
您需要创建一个<一个href=\"http://msdn.microsoft.com/en-us/library/microsoft.reporting.winforms.reportdatasource.value%28v=vs.80%29.aspx\">ReportDataSource,并设置其值
属性 - 例如数据表
和的IEnumerable
是的支持来源
You need to create a ReportDataSource, and set its Value
property - e.g. DataTable
and IEnumerable
s are supported sources
作为一个例子,假设一个方法存在返回数据集,有一个数据表
与之相匹配的报告所需的列:
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行动
为嵌入的资源
,然后你可以设置 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屋!