问题
I have a LocalReport
object that I fill with all the apropriate information. I use this same report object to export to different formats. My users can select Image, Excel, Word, Pdf, etc. and I use the same report object to facilitate those request.
My issue is sometimes they may want to view it. I know I can open the exported type but that is not what I want to happen. I want to view it in a ReportViewer
. I know I can set ReportViewer.LocalReports
properties and get what I'm looking for , but I already set everything up in my Report object.
So the question is: How do I do the followin which is incorrect and can't be done?
LocalReport _Report = new LocalReport();
//set all my report information
Microsoft.Reporting.WinForms.ReportViewer _rv = new Microsoft.Reporting.WinForms.ReportViewer();
//This is what I'm trying to do
_rv.LocalReport = _Report;
回答1:
You can try by altering the order of things you are currently doing.
Add a ReportViewer to the form. (I am not sure why you are creating the ReportViewer in code. I believe you are not going to dynamically add it to the controls of the form.)
Set all your report information in the ReportViewer.LocalReport object. No need to create it like you have done it in your first line of code.
Call ReportViewer.RefreshReport() method to render the report on the form.
PS: If you already have a LocalReport object, you will have to assign the properties from that to the report object on the ReportViewer.
回答2:
Like you, I wanted to be able to show a LocalReport in a ReportViewer.
Here how I've achieved this :
Param_MyLocalReport is the LocalReport that is working well [with .Render]. ReportViewer1 is, eh, the ReportViewer where I want to show my report. This function is automatic, an will copy data-sources and parameters.
//****************************
//assign report Path
reportViewer1.LocalReport.ReportPath = param_MyLocalReport.ReportPath;
//****************************
//****************************
//assign data-sources
foreach (ReportDataSource MyDS in param_MyLocalReport.DataSources)
reportViewer1.LocalReport.DataSources.Add(MyDS);
//****************************
//****************************
//Assign parameters
//get a list of actual parameters in the report, with the actual assigned value
ReportParameterInfoCollection MyOrigParams = param_MyLocalReport.GetParameters(); //I didn't find simpler way to fetch params...
//create a List of parameter [to feed the reportViewer]
List<ReportParameter> MyListOfPArams = new List<ReportParameter>();
//for each params found through GetParameters(), add it to the List<> of params
for (int i = 0; i < MyOrigParams.Count; i++)
MyListOfPArams.Add(new ReportParameter(MyOrigParams[i].Name, MyOrigParams[i].Values[0]));
//final assignation of the parameters
reportViewer1.LocalReport.SetParameters(MyListOfPArams);
//****************************
//show the report
reportViewer1.RefreshReport();
Like El Nino mentionned, this can be pushed in an helper fonction. Something like :
Private void Convert_LocalReport_To_ReportViewer(LocalReport Param_MyLocalReport, ReportViewer param_MyReportViewer)
{
...copy the same code here...
}
回答3:
You can process the report in more than one processing mode. The following code shows you the processing mode is Local.
_RptViewer.ProcessingMode=ProcessingMode.Local;
// _RptViewer is the name of the Report Viewer Control added to your Page/Form.
LocalReport objRpt=_RptViewer.LocalReport;
objRpt.ReportPath=Server.MapPath("YourReportName.rdlc");
ReportDataSource rds=new ReportDataSource("DataSourceName",DataSourceObject);
//"DataSourceName" can be the name of the DataSet you created during designing the Report;
//and DataSourceObject can be a method that returns a data table or DataTable that is defined in your code above, or any valid object that provides data to the report.*
objRpt.DataSources.Clear();
objRpt.DataSources.Add(rds);
I hope the above code sample helps you out.
回答4:
You might be able to get away with using reflection to set the LocalReport on a ReportViewer but be warned that this could cause issues. I'm doing this on a project right now and it seems to work well. See my answer here: https://stackoverflow.com/a/14329386/285874
来源:https://stackoverflow.com/questions/9200308/vs2010-rdlc-c-how-can-i-set-a-localreport-object-to-a-reportviewer