Leveraging the ReportViewer control in WPF

Microsoft's ReportViewer control is an ideal candidate for embedding reports in a WinForms application. It takes care of the generation, rendering, printing, and saving of reports, even if you don't host these in SQL Server Reporting Services. The ReportViewer only exists for WinForms and ASP.NET: there is no ReportViewer control for WPF. If you migrate a WinForms application to WPF, you can however leverage your existing reports and skills.

Any Win32 Control can be embeded in a WPF form by hosting it in a WindowsFormsHost element. All you need to do is add a reference in your project to Microsoft.ReportViewer.WinForms, and wrap the ReportViewer control in a WindowsFormsHost element, like this:

<Window x:Class="ReportViewerSample.Window1"
        Title="ReportViewer Sample" Height="800" Width="500">
        <WindowsFormsHost x:Name="windowsFormsHost1">
            <rep:ReportViewer x:Name="ReportViewer1"></rep:ReportViewer>

Unfortunately Visual Studio's designers and code generators won't help you in hooking up the datasets, the resources, and the report itself to the ReportViewer, so you have to do it all programmatically. Here's how your code could look like:

// Create and Fill DataSet(s)
AdventureWorksDataSet ads = new AdventureWorksDataSet();
SalesOrderDetailTableAdapter adapter = new SalesOrderDetailTableAdapter();
// Attach DataSource(s) to ReportViewer
ReportDataSource rds = new ReportDataSource();
rds.Name = "AdventureWorksDataSet_SalesOrderDetail";
rds.Value = ads.SalesOrderDetail; // Could also come from a WCF Service
// Attach Report to ReportViewer
ReportViewer1.LocalReport.ReportEmbeddedResource = "ReportViewerSample.SalesReport.rdlc";
// Alternative: ReportViewer1.LocalReport.ReportPath = @"../../SalesReport.rdlc";
// Populate Parameters
List<ReportParameter> parms = new List<ReportParameter>();
parms.Add(new ReportParameter("Report_Parameter_0", "25"));
// Display


  1. When i want to ReportViewer1.LocalReport.SetParameters(parms);
    it cause exception like this:
    Cannot create instance of ' ' defined in assembly ' Version=, Culture=neutral, PublicKeyToken=null'. Exception has been thrown by the target of an invocation. Error in markup file 'Wizards/Print.xaml' Line 1 Position 9."
    is there any solution for this?

  2. you saved my life hehehheeh
    thank you

  3. Know of any plans for a native WPF report viewer? We currently use WinFormsHost but it causes havoc on the display of our other controls for various reasons (Can't move other controls over the top of it, no hardware acceleration, etc.)?

  4. Below is the code i am trying make use of for displaying a report in WPF Window.

    With WindowsFormsHost and report viewer control.

    Problem : it just displays a Blank Window. it gives No Errors, No messages.

    Just displays a blank window. please help.

    XAML Code

    Code Behind
    private void Window_Loaded(object sender, RoutedEventArgs e)
    List paramList = new List();
    paramList.Add(new ReportParameter("param1","16699"));
    reportViewer.ProcessingMode = ProcessingMode.Remote;
    reportViewer.ServerReport.ReportServerUrl = new Uri("http://server/reportserver");
    reportViewer.ServerReport.ReportPath = "/folder1/folder2/reportname";
    catch (Exception ex)

  5. Thank you !.!.!.

  6. Hi Diederik!

    Thanks a lot for this code. I had the same requirement, and used your code for embedding a report viewer in the form. However, when i click on the button to view the report, it doesn't reflect anything, nor does it throw any runtime errors.

    Could you help me with some inputs that i need to look at? I tried creating a simple win form app and pointed the report in the report viewer of the same, and it worked. Thus, i know that there is no issue with the report, since this works independently fine.

  7. Hi Again, Diederik! It was a silly mistake, I was using the wrong refresh() function. When I used RefreshReport(), it worked!

  8. // Attach Report to ReportViewer

    ReportViewer1.LocalReport.ReportEmbeddedResource = "ReportViewerSample.SalesReport.rdlc";

    // Alternative: ReportViewer1.LocalReport.ReportPath = @"../../SalesReport.rdlc";

    these lines are not working

    pls help

  9. If you want your ex-girlfriend or ex-boyfriend to come crawling back to you on their knees (no matter why you broke up) you must watch this video
    right away...

    (VIDEO) Get your ex back with TEXT messages?

  10. BlueHost is ultimately one of the best hosting provider with plans for any hosting requirements.