Saturday, June 25, 2011

WPF - RDLC Report in WPF

In this article i will show you how to generate RDLC report in WPF application.

Step 1
Download northwind database from the following link.
http://www.microsoft.com/downloads/details.aspx?FamilyID=06616212-0356-46a0-8da2-eebc53a68034&displaylang=en

Step 2
Attach a northwind database into MS-SQL server

Step 3
Create a WPF application and give solution name as SolRDLCReportWPF.


Step 4
First we need to create Report Viewer in WPF Application.Unfortunately there is no ReportViewer control for WPF application but ReportViewer only exists in WinForms and ASP.NET.So we can used WindowsFormsHost to integrate the report viewer control with the WPF form.
Drag and drop WindowFormHostControl on window from Toolbox.it is look like this






Click on image for better view


<Grid>
        <WindowsFormsHost x:Name="WinFormHost">
            
        </WindowsFormsHost>
 </Grid>

Step 5
Add Microsoft.ReportViewer.WinForms assembly reference to the project from solution explorer,it is look like this




Click on image for better view

Note : Select only Version 10.0.0.0

Step 6
Add a ReportViewer assembly reference in window tag,it is look like this


xmlns:RV="clr-namespace:Microsoft.Reporting.WinForms;assembly=Microsoft.ReportViewer.WinForms"

Finally it's look like this
<Window x:Class="SolRDLCReportWPF.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">

Step 7
Wrap the ReportViewer control in a WindowsFormsHostelement,it is look like this
<Grid>
        <WindowsFormsHost x:Name="WinFormHost">
            <RV:ReportViewer x:Name="ReportEmployee"></RV:ReportViewer>
        </WindowsFormsHost>
</Grid>

Finally Presentation part done now we Create DataSet Schema and Report Design.

Step 8
First We create a DataSet Schema.it can be define Dataset schema without connecting to any datasource.
Add a DataSet Schema,right click on Add new Item,select DataSet from installed Visual Studio templates and name it NorthwindDataSet and click on add button,it is look like this




Click on image for better view

Step 9
Click on toolbox icon,it is look like this



Click on image for better view

Select DataTable from Toolbox and drag and drop to the dataset design editor,it is look like this




Finally Add column to schema,it is look like this



Click on image for better view

DataSet Schema is ready now we create Report Design in WPF

Step 10
Add a RDLC Report,right click on solution,select  Add new Item,select Report from installed Visual Studio templates and name it NorthwindReport and click on add button,it is look like this



Click on image for better view

Step 11
Add DataSet Schema to the report,it is look like this



Click on image for better view

In the next dialog, give the dataset a name called EmployeeDataSet. Change the data source to NorthwindDataSet and select available dataset Employee and click OK,it is look like this 



Click on image for better view

Step 12
Add Header and Footer on report,it is look like this



Click on image for better view

In Header Section Add TextBox from toolbox,it is look like this



Click on image for better view

In Footer Section Add Page number from build in field,it is look like this



Click on image for better view

Step 13
Add Table from toolbox for display employee data,it is look like this



Click on image for better view

Drag and Drop all Employee Fields from NorthwindDataSet into table,it is look like this




Click on image for better view

Finally Report is ready now we move to programming part.

Step 14
Bind Employee data to Dataset Schema,it is look like this
 #region Bind Employee Data to DataSet Schema
        /// 
        /// Get Employee data from Northwind database and bind in NorthwindDataSet
        /// 
        /// DataTable
        private DataTable GetEmployeeData()
        {
            try
            {
                // Open Sql Connection
                SqlConnection SqlCon = new SqlConnection(@"Data Source=SHREE\SHREE;Initial Catalog=Northwind;Integrated Security=True");
                SqlCon.Open();

                // Create a Command
                SqlCommand SqlComm = new SqlCommand();
                SqlComm.Connection = SqlCon;
                SqlComm.CommandType = CommandType.Text;
                SqlComm.CommandText = "SELECT FirstName,LastName,BirthDate,Address,City,PostalCode,Country FROM Employees";

                // Create instance of Northwind DataSetXSD
                NorthwindDataSet.EmployeeDataTable EmployeeDt = new NorthwindDataSet.EmployeeDataTable();

                // Set a Data Commands
                SqlDataAdapter SqlDa = new SqlDataAdapter(SqlComm);
                SqlDa.Fill(EmployeeDt); // Fill Data in NorthwindDataSet Object.

                return EmployeeDt;

            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }

        #endregion

Step 15
Display Report in Report Viewer,it is look like this
#region Display Report
        /// 
        /// Display Report in Report Viewer
        /// 
        private void DisplayReport()
        {
            try
            {
                // Set a DataSource to the report

                // First Parameter - Report DataSet Name
                // Second Parameter - DataSource Object i.e DataTable
                ReportEmployee.LocalReport.DataSources.Add(new ReportDataSource("EmployeeDataSet", GetEmployeeData()));

                // Set A Report Embedded Resource
                ReportEmployee.LocalReport.ReportEmbeddedResource = "SolRDLCReportWPF.NorthwindReport.rdlc";
                // OR Set Report Path
                // Alternative: ReportEmployee.LocalReport.ReportPath = @"../../NorthwindReport.rdlc";

                // Display Report
                ReportEmployee.RefreshReport();
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);  
            }
        }

        #endregion

Call DisplayReport function on window constructor,it is look like this
 public MainWindow()
        {
            InitializeComponent();

            try
            {
                DisplayReport(); 
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);  
            }
        }

Run the project.

Output



Click on image for better view

Full Code

1. XAML Code
<Window x:Class="SolRDLCReportWPF.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 x:Name="WinFormHost">
            <RV:ReportViewer x:Name="ReportEmployee"></RV:ReportViewer>
        </WindowsFormsHost>
    </Grid>
</Window>

2. Code Behind
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Data;
using System.Data.SqlClient;
using Microsoft.Reporting.WinForms;


namespace SolRDLCReportWPF
{
    /// 
    /// Interaction logic for MainWindow.xaml
    /// 
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            try
            {
                DisplayReport(); 
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);  
            }
        }

        #region Bind Employee Data to DataSet Schema
        /// 
        /// Get Employee data from Northwind database and bind in NorthwindDataSet
        /// 
        /// DataTable
        private DataTable GetEmployeeData()
        {
            try
            {
                // Open Sql Connection
                SqlConnection SqlCon = new SqlConnection(@"Data Source=SHREE\SHREE;Initial Catalog=Northwind;Integrated Security=True");
                SqlCon.Open();

                // Create a Command
                SqlCommand SqlComm = new SqlCommand();
                SqlComm.Connection = SqlCon;
                SqlComm.CommandType = CommandType.Text;
                SqlComm.CommandText = "SELECT FirstName,LastName,BirthDate,Address,City,PostalCode,Country FROM Employees";

                // Create instance of Northwind DataSetXSD
                NorthwindDataSet.EmployeeDataTable EmployeeDt = new NorthwindDataSet.EmployeeDataTable();

                // Set a Data Commands
                SqlDataAdapter SqlDa = new SqlDataAdapter(SqlComm);
                SqlDa.Fill(EmployeeDt); // Fill Data in NorthwindDataSet Object.

                return EmployeeDt;

            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }

        #endregion

        #region Display Report
        /// 
        /// Display Report in Report Viewer
        /// 
        private void DisplayReport()
        {
            try
            {
                // Set a DataSource to the report

                // First Parameter - Report DataSet Name
                // Second Parameter - DataSource Object i.e DataTable
                ReportEmployee.LocalReport.DataSources.Add(new ReportDataSource("EmployeeDataSet", GetEmployeeData()));

                // Set A Report Embedded Resource
                ReportEmployee.LocalReport.ReportEmbeddedResource = "SolRDLCReportWPF.NorthwindReport.rdlc";
                // OR Set Report Path
                // Alternative: ReportEmployee.LocalReport.ReportPath = @"../../NorthwindReport.rdlc";

                // Display Report
                ReportEmployee.RefreshReport();
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);  
            }
        }

        #endregion
    }
}


Download
Download Source Code

45 comments:

  1. Nice article kishor.

    ReplyDelete
  2. Thank you so much Kishor... :)

    binil@msn.com

    ReplyDelete
  3. Thank you for this great article.

    Andreea

    ReplyDelete
  4. Thanks a lot for giving nice document

    ReplyDelete
  5. Very very nice and Easy to understand..I love this <3
    Thanks a lot broo :)

    ReplyDelete
    Replies
    1. Most Welcome Brother.
      Thanks for you like my article.

      Delete
  6. Replies
    1. Check the following Link
      http://msdn.microsoft.com/en-us/library/ms252091.aspx

      Delete
  7. Good article !! One should appreciate ur effort ..Thanks !!

    ReplyDelete
  8. Thanks, helped a lot.

    ReplyDelete
  9. nice and useful article dear

    ReplyDelete
  10. Nice Work Brother ... Keep on Goin :-)

    ReplyDelete
  11. Very helpful post brother, many many thanks, how can i add some parameters like year, search by date, employee ?

    ReplyDelete
  12. Bro it's telling "A data source instance has not been supplied for the data source 'DataSet1' "

    ReplyDelete
  13. Simple and easy to understand :) thank you kishore

    ReplyDelete
  14. Kishor Thanks for help I need it in MVVM way can it be possible

    ReplyDelete