Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visual Studio LocalReport Object and ReportViewer

Is there a way to process a LocalReport object (got this part done) and get it displayed after, in a ReportViewer control, on another form? The idea is to print without the ReportViewer (already done) but, if the user wants to can also preview what he is about to print.

I'm using Visual Basic .NET SDK 3.5 and Visual Studio 2008. Can also use 2010, if needed.

I tryed to do something like this:

ReportViewer1.LocalReport = myLocalReport 

but without luck since LocalReport property on ReportViewer is read only...

Any hint on this? Thanks in advance.

(I know to preform this using the ReportViewer1.LocalReport method. All I want is to create a single code and bind it either to the printer directly or to the preview form)

like image 264
Mário Rodrigues Avatar asked Apr 27 '26 21:04

Mário Rodrigues


1 Answers

I have a similar situation to you were I have services that can create a Local Report which can then be generated into a PDF, emailed, etc. However, because ReportViewer.LocalReport is a read-only property I had to either duplicate the code used to build the report or copy the values from my LocalReport to the ReportViewer.LocalReport. I'm not a fan of either option because either something might not get copied (i.e. sub reporting event) or there is code duplication.

I came up with the following extension that sets the LocalReport on a ReportViewer with reflection. I have not fully tested this and it may be a bad idea! However, it seems to work for the project I'm currently working on. I have no idea if the ReportViewer does some additional initialization of it's local report so something might blow up....

I CAN'T STRESS THIS ENOUGH -- USE AT YOUR OWN RISK - PROBABLY NOT A GOOD IDEA TO DO THIS

public static class ReportViewerExtensions
{
    public static void SetLocalReport(this ReportViewer reportViewer, LocalReport report)
    {
        var currentReportProperty = reportViewer.GetType().GetProperty("CurrentReport", BindingFlags.NonPublic | BindingFlags.Instance);
        if (currentReportProperty != null)
        {
            var currentReport = currentReportProperty.GetValue(reportViewer, null);
            var localReportField = currentReport.GetType().GetField("m_localReport", BindingFlags.NonPublic | BindingFlags.Instance);
            if (localReportField != null)
            {
                localReportField.SetValue(currentReport, report);
            }
        }
        reportViewer.RefreshReport();
    }
}

Usage:

LocalReport localReport = reportService.GenerateCurrentOrdersReport(....);
reportViewer.SetLocalReport(localReport);
like image 176
Mike Rowley Avatar answered Apr 29 '26 22:04

Mike Rowley



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!