Report Viewer in WPF

In the following article I am going to share my experience of using ReportViewer Control with WPF.

First of all let's have a small discussion on ReportViewer Control.


ReportViewer is a freely redistributable control that enables embedding reports in applications developed using the .NET Framework. We can easily design the reports with drag-n-drop simply using Report Designer included in Visual Studio 2010.

The benefits that are offered by ReportViewer Control:

  1. The reporting engine built into ReportViewer can perform operations such as filtering, sorting, grouping and aggregation. And hence processes data efficiently
  2. To present data it supports a variety of ways. So that the data can be present as lists, tables, charts and matrices (also known as crosstabs.)
  3. It adds visual appeal. This means we can specify fonts, colors, border styles, background images etc. to make our report visually appealing.
  4. Enables interactivity in reports. We can have collapsible sections, document map, bookmarks, interactive sorting etc. in our report.
  5. Conditional formatting is also supported by report viewer. We can embed expressions in the report to change display style dynamically based on data values.
  6. Supports printing and print preview.
  7. Supports export to Excel, Word and PDF formats.

Generally we follow the usage of the Report Viewer in Windows Form programs. But here we are going to use this in WPF application.

Step by step creation of report using ReportViewer Control in WPF application

1. Very first step is to open a new project of WPF Application project type and name it to "ReportViewerWPF".


2. Secondly, click on the Toolbox, and drag a "WindowsFormsHost" control onto the design surface for MainWindow.xaml. This also adds the assemblies that are needed by WindowsFormstHost to our project.



<WindowsFormsHost Height="100" Width="100" Name="windowsFormsHost1"/>

3. Now, in Solution Explorer, right-click on the project and select Add Reference.


4. In the .NET tab of the Add Reference dialog log, select the Microsoft.ReportViewer.WinForms assembly, and click OK. After adding successfully it shows in the solution explorer pane under the head "Refrences".



5. Also add the highlighted lines to your code:

   <Window x:Class="ReportViewerWPF.MainWindow"
   Title="MainWindow" Height="300" Width
        <WindowsFormsHost Name="windowsFormsHost1">
            <rv:ReportViewer x:Name="_reportViewer"/>

This creates an instance of ReportViewer named _reportViewer in the XAML page.

6. Now let's create an RDLC report that you will display in the ReportViewer control. And for this, In Solution Explorer, right-click your project, point to Add, and select New Item.


7. In the Add New Item dialog box, select Report Wizard which is in Reporting template, and click Add. This will open the Report Wizard with the Data Source Configuration Wizard like this:



8. In the Data Source Configuration Wizard, select your Database and click Next, then again select Dataset, and click Next.



9. After clicking on Next, Choose Your Data Connection page will be open, click New Connection. If you see the Choose Data Source dialog box, select Microsoft SQL Server and click Continue.


10. In the Server name box, type the server name that hosts your database that you are going to select, as I have used the localhost and for this I have to log on to my server and in "Select or enter a database" name, select the database which you are going to use, and test your connection before clicking OK.


11. Click Next button twice, so that the page "Choose your Database Objects" will be opened .

12. In the Choose Your Database Objects page, expand the Tables node, select the checkbox for the table for which you want to create a report, and click Finish.


13. A DataSet object called YourDatabaseNameDataSet is now created in your project.


14. In the Report Wizard, click Next.

15. In the Arrange Fields page, drag all the available fields and drop to the Values pane. This creates a simple tabular table for displaying the sample data. Then, click Next two times and lastly click on Finish to close the Report Wizard.


16. Now your window of report.rdlc will look like as under:


17. Next, it's time to add the code to point ReportViewer to the new report that we have created and also to add data from "YourDatabaseNameDataSet" to ReportViewer.
18. Open MainWindow.xaml.cs, and add the following lines:


namespace ReportViewerWPF

    public partial class MainWindow : Window
       public MainWindow()
            _reportViewer.Load += ReportViewer_Load;
        private bool _isReportViewerLoaded;

        private void ReportViewer_Load(object sender, EventArgs e)
            if (!_isReportViewerLoaded)
                Microsoft.Reporting.WinForms.ReportDataSource reportDataSource1 = new      
                WpfApplication4DataSet dataset = new WpfApplication4DataSet();


                reportDataSource1.Name = "DataSet1";
//Name of the report dataset in our .RDLC file

                reportDataSource1.Value = dataset.Accounts;

                this._reportViewer.LocalReport.ReportPath = "../../Report.rdlc";                    

                //fill data into WpfApplication4DataSet
                accountsTableAdapter = new   
                accountsTableAdapter.ClearBeforeFill = true;
                _isReportViewerLoaded = true;