C# 为本地报表设置数据源 - .NET & Report Viewer
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8802707/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Setting the datasource for a Local Report - .NET & Report Viewer
提问by user559142
I have created a custom control (a windows form with a report viewer). I have the following code to load a local report:
我创建了一个自定义控件(带有报表查看器的 Windows 窗体)。我有以下代码来加载本地报告:
Contained in CustomReportViewer Class
包含在 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);
}
I call this using:
我称之为使用:
CustomReportViewer reportViewer = new CustomReportViewer();
This works fine and a windows form appears containing the report viewer control butI 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?
我不完全确定如何设置数据源?我需要的数据存储在远程数据库中……我需要做什么来建立这个连接?
回答by StuartLC
You need to create a ReportDataSource, and set its Valueproperty - e.g. DataTableand IEnumerables are supported sources
您需要创建一个ReportDataSource,并设置其Value属性 - 例如DataTable和IEnumerables 是支持的来源
As an example, and assuming that a method exists to return a DataSet, with a single DataTablematching the columns needed by your report:
举个例子,假设存在一个方法来返回一个DataSet,有一个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();
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 Actionon the RDLC as Embedded Resource, and then you can set the ReportEmbeddedResourceproperty:
请注意,将 RDLC 嵌入到您的程序集中通常更容易,而不必保留单独的 RDLC 文件。通过Build Action将 RDLC 上的选择为来执行此操作Embedded Resource,然后您可以设置该ReportEmbeddedResource属性:
reportViewer1.LocalReport.ReportEmbeddedResource =
"MyOrganisation.MyAssembly.NameSpace.MyReportName.rdlc";
Note that the resource string must include the fully qualified name of the resource (including Assembly).
请注意,资源字符串必须包含资源的完全限定名称(包括程序集)。
回答by Anthony Griggs
The key for me was as answered by StuartLC as above... with further clarification in that when he said it "Must match the DataSource in the RDLC".. it actually turned out to be the "DataSetName" element value re: <DataSetName>DataSet1</DataSetName>
对我来说,关键是由 StuartLC 如上所述回答......进一步澄清,当他说它“必须匹配 RDLC 中的数据源”时......它实际上是“DataSetName”元素值: <DataSetName>DataSet1</DataSetName>
I went round and round because it is called "DataSource" so I kept using the DataSource element name but apparently in the rdl and rdlc file this really signifies the DataSetName. So keeping that in mind here is the code as borrowed from Stuart above with my own. Note the DataSetName element value:
我绕来绕去,因为它被称为“数据源”,所以我一直使用数据源元素名称,但显然在 rdl 和 rdlc 文件中,这确实表示数据集名称。所以请记住,这里是从上面 Stuart 借用我自己的代码。请注意 DataSetName 元素值:
using (SqlConnection sqlConn = new SqlConnection(rvConnection))
using (SqlDataAdapter da = new SqlDataAdapter(rvSQL, rvConnection))
{
DataSet ds = new DataSet();
da.Fill(ds);
DataTable dt = ds.Tables[0];
this.reportViewer1.Reset();
this.reportViewer1.ProcessingMode = ProcessingMode.Local;
this.reportViewer1.LocalReport.ReportPath = AppDomain.CurrentDomain.BaseDirectory + "ssrsExport.rdlc";
ReportDataSource reportDataSource = new ReportDataSource();
// Must match the DataSet in the RDLC
reportDataSource.Name = "DataSet1";
reportDataSource.Value = ds.Tables[0];
this.reportViewer1.LocalReport.DataSources.Add(reportDataSource);
this.reportViewer1.RefreshReport();
}
回答by Shane.A
you may be able to just use a datatable instead of a dataset. change this:
您可以只使用数据表而不是数据集。改变这个:
DataSet ds = new DataSet();
da.Fill(ds);
DataTable dt = ds.Tables[0];
to this:
对此:
DataTable dt = new DataTable();
da.Fill(dt);
and this:
和这个:
reportDataSource.Value = ds.Tables[0];
to this
对此
reportDataSource.Value = dt;

