Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
340 views
in Technique[技术] by (71.8m points)

c# - Setting the datasource for a Local Report - .NET & Report Viewer

I have created a custom control (a windows form with a report viewer). I have the following code to load a local report:

Contained in CustomReportViewer Class

//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 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You need to create a ReportDataSource, and set its Value property - e.g. DataTable and IEnumerables 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();

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).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...