Tuesday, August 26, 2008

Report Viewer in WPF application

I have started working on a WPF application which has to display some data in a graphical manner. Instead of using the different available charting tools, I decided to use the report facility inbuilt in the Visual Studio to display .
Visual Studio allows for binding objects as the report's data source to create an local report. On trying to achieve my purpose, I came across a few difficulties and on spending some time through various posts and forums I got the basic direction, so thought of consolidating my learning and putting it up.

The problems I came across were as folows:


  • Report viewer is not available as a native control in WPF applications, and therefore it could not be placed in the application.

  • Binding the data source (the object) to the report.

To solve these couple of issues, I went about it in the following way:



  • Since the report viewer is not available on WPF, but on Windows Forms - I used WindowsFormsHost to integrate the report viewer control with the WPF form. WindowsFormsHost is the implementation from Microsoft, which would have other wise required me to derive a class from the HwndHost base class, to add a HWND to my WPF application to host Windows Form control.


  1. I added the following references to my project: Microsoft.ReportViewer.Common, Microsoft.ReportViewer.WinForms, System.Windows.Forms, WindowsFormIntegration.

  2. Create an object of ReportViewer and specify the processing mode as local, the report path (the .rdlc path)

  3. Create an object of the ReportDataSource with the name of the data source and the object which contains the data as its parameters.

  4. Add the data source to the Local report created.

  5. Refresh the report.

  6. Create WindowsFormsHost object which is used for inserting the report to the control on the WPF form.
Sample Code Snippet (Window1.xaml.cs):

//Merchant is the class used to enter new product names and their prices.
Merchant merchant = new Merchant(); List products = new List(); products = merchant.GetProducts(); WindowsFormsHost host = new WindowsFormsHost(); Microsoft.Reporting.WinForms.ReportViewer reportViewer = new Microsoft.Reporting.WinForms.ReportViewer();
//Specifying local processing mode for the ReportViewer. reportViewer.ProcessingMode = ProcessingMode.Local;

//Specifying the location of the report definition file. reportViewer.LocalReport.ReportPath = "..\\..\\ProductReport.rdlc";

//Creating a new ReportDataSource with the name of the DataSource and the object // which is to be used as the DataSource. ReportDataSource ds = new ReportDataSource("ReportViewer_Product", products);

//Adding the ReportDataSource to the DataSoure of the ReportViewer reportViewer.LocalReport.DataSources.Add(ds);

//Causes the current report in the Report Viewer to be processed and rendered. reportViewer.RefreshReport();

//Sets the child control hosted by the WindowsFormsHost element. host.Child = reportViewer;
//Add the WindowsFormsHost element to the Grid in the Window1.xaml reportGrid.Children.Add(host);


The final result will look something like this, other items can also be used instead of a table which has been placed here: