ARTICLE

Report Viewer in WPF

Posted by Nipun Tomar Articles | WPF December 22, 2011
In the following article, I am going to share my experience of using ReportViewer Control in a WPF.
Reader Level:

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

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

RptWPF1.jpg

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.

RptWPF2.jpg

Code

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

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

RptWPF3.jpg

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

RptWPF4.jpg

RptWPF5.jpg

5. Also add the highlighted lines to your code:

   <Window x:Class="ReportViewerWPF.MainWindow"
   xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
   xmlns:x
="http://schemas.microsoft.com/winfx/2006/xaml"
   xmlns:rv="clr-namespace:Microsoft.Reporting.WinForms;
             assembly=Microsoft.ReportViewer.WinForms"
   Title="MainWindow" Height="300" Width
="400">
     <Grid>
        <WindowsFormsHost Name="windowsFormsHost1">
            <rv:ReportViewer x:Name="_reportViewer"/>
        </WindowsFormsHost>
     </Grid>
   </
Window>

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.

RptWPF6.jpg

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:

RptWPF7.jpg

RptWPF8.jpg

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

RptWPF9.jpg

RptWPF10.jpg

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.

RptWPF11.jpg

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.

RptWPF12.jpg

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.

RptWPF13.jpg

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

RptWPF14.jpg

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.

RptWPF15.jpg

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

RptWPF16.jpg

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:

MainWindow.xaml.cs:

namespace ReportViewerWPF

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

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

                dataset.BeginInit();

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

                reportDataSource1.Value = dataset.Accounts;
                this._reportViewer.LocalReport.DataSources.Add(reportDataSource1);

                this._reportViewer.LocalReport.ReportPath = "../../Report.rdlc";                    
                dataset.EndInit();

                //fill data into WpfApplication4DataSet
                WpfApplication4DataSetTableAdapters.AccountsTableAdapter
                accountsTableAdapter = new   
                WpfApplication4DataSetTableAdapters.AccountsTableAdapter();
 
                accountsTableAdapter.ClearBeforeFill = true;
                accountsTableAdapter.Fill(dataset.Accounts);                    
                _reportViewer.RefreshReport();
                _isReportViewerLoaded = true;
            }
        }
    }
}

Article Extensions
Contents added by enric vives on Sep 12, 2013

Hi ya, I've tested your code in my IDE using Vb .Net (Framework 4.5) and for some reason doesn't work.

Project is uploaded in my skydrive: https://skydrive.live.com/?cid=3b225f20fcf340c8&id=3B225F20FCF340C8!223

 reportviewer.png


Vs 2012 ultimate Update 3.
Microsoft.ReportViewer.WinForms.dll
ver: 11.0.0.0
rutime ver: v2.0.50727

xaml:

<Window x:Class="MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:rv="clr-namespace:Microsoft.Reporting.WinForms;assembly=Microsoft.ReportViewer.WinForms"
       
    Title="MainWindow" Height="350" Width="525">
    <Grid>
        <WindowsFormsHost Margin="48,26,50,110" >
            <rv:ReportViewer x:Name="_reportViewer"/>
        </WindowsFormsHost>
    </Grid>
</Window>


VB code-behind:

Imports System.Data.Sql
Imports System.Data.SqlClient

Class MainWindow

    Public _isReportViewerLoaded As Boolean

    Public Sub New()

        InitializeComponent()


        AddHandler _reportViewer.Load, AddressOf altre
    End Sub


    Public Sub altre(sender As Object, e As EventArgs)
        Dim reportDataSource1 As New Microsoft.Reporting.WinForms.ReportDataSource()

        Dim WpfApplication4DataSet As New TalleresDataSet1


        WpfApplication4DataSet.BeginInit()

        reportDataSource1.Name = "Dataset2"

        reportDataSource1.Value = WpfApplication4DataSet.Talleres_Articulos


        Me._reportViewer.LocalReport.DataSources.Add(reportDataSource1)

        ' Me._reportViewer.LocalReport.ReportEmbeddedResource = "ReportViewerWPF.Report2.rdlc"

        Me._reportViewer.LocalReport.ReportPath = "../../Report2.rdlc"


        WpfApplication4DataSet.EndInit()

        Dim accountsTableAdapter As New TalleresDataSet1TableAdapters.Talleres_ArticulosTableAdapter
        accountsTableAdapter.ClearBeforeFill = True

        accountsTableAdapter.Fill(WpfApplication4DataSet.Talleres_Articulos)

        _reportViewer.RefreshReport()

        _isReportViewerLoaded = True






    End Sub

End Class

Contents added by Ma Khanh Xuyen on Jan 29, 2013
Very good.....
COMMENT USING
Employers - Post Free Jobs