我创建了一个自定义控件(带有报告查看器的窗口窗体)。我有以下代码来加载本地报告:
包含在 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();
这工作正常,并且会出现一个包含报表查看器控件的窗口窗体but我收到以下消息:
A data source instance has not been supplied for the data source "ReportData"
我不完全确定如何设置数据源?我需要的数据存储在远程数据库中...我需要做什么才能设置此连接?
你需要创建一个报表数据源 http://msdn.microsoft.com/en-us/library/microsoft.reporting.winforms.reportdatasource.value%28v=vs.80%29.aspx,并设置其Value
财产 - 例如DataTable
and IEnumerable
s are 支持的来源 http://msdn.microsoft.com/en-us/library/ms252094.aspx
作为一个例子,假设存在一个方法来返回DataSet http://msdn.microsoft.com/en-us/library/bh8kx08z(v=vs.100).aspx,与单个DataTable
匹配您的报告所需的列:
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 文件更容易。通过选择Build Action
在 RDLC 上为Embedded Resource
,然后您可以设置ReportEmbeddedResource
财产:
reportViewer1.LocalReport.ReportEmbeddedResource =
"MyOrganisation.MyAssembly.NameSpace.MyReportName.rdlc";
请注意,资源字符串必须包含资源的完全限定名称(包括程序集)。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)