Search code examples
visual-studio-2010reportviewerrdlclocalreport

VS2010 RDLC C#. How can I set a LocalReport object to a ReportViewer?


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;

Solution

  • You can try by altering the order of things you are currently doing.

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

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

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