WPF – RDLC Report in WPF

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

  1. <Grid>  
  2.         <WindowsFormsHost x:Name=“WinFormHost”>  
  3.               
  4.         </WindowsFormsHost>  
  5.  </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

  1. xmlns:RV=“clr-namespace:Microsoft.Reporting.WinForms;assembly=Microsoft.ReportViewer.WinForms”  

Finally it’s look like this

  1. <Window x:Class=“SolRDLCReportWPF.MainWindow”  
  2.         xmlns=http://schemas.microsoft.com/winfx/2006/xaml/presentation&#8221;  
  3.         xmlns:x=http://schemas.microsoft.com/winfx/2006/xaml&#8221;  
  4.         xmlns:RV=“clr-namespace:Microsoft.Reporting.WinForms;assembly=Microsoft.ReportViewer.WinForms”  
  5.         Title=“MainWindow” Height=“350” Width=“525”>  

Step 7
Wrap the ReportViewer control in a WindowsFormsHostelement,it is look like this

  1. <Grid>  
  2.         <WindowsFormsHost x:Name=“WinFormHost”>  
  3.             <RV:ReportViewer x:Name=“ReportEmployee”></RV:ReportViewer>  
  4.         </WindowsFormsHost>  
  5. </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

  1. #region Bind Employee Data to DataSet Schema  
  2.        /// <summary>  
  3.        /// Get Employee data from Northwind database and bind in NorthwindDataSet  
  4.        /// </summary>  
  5.        /// <returns>DataTable</returns>  
  6.        private DataTable GetEmployeeData()  
  7.        {  
  8.            try  
  9.            {  
  10.                // Open Sql Connection  
  11.                SqlConnection SqlCon = new SqlConnection(@“Data Source=SHREE\SHREE;Initial Catalog=Northwind;Integrated Security=True”);  
  12.                SqlCon.Open();  
  13.   
  14.                // Create a Command  
  15.                SqlCommand SqlComm = new SqlCommand();  
  16.                SqlComm.Connection = SqlCon;  
  17.                SqlComm.CommandType = CommandType.Text;  
  18.                SqlComm.CommandText = “SELECT FirstName,LastName,BirthDate,Address,City,PostalCode,Country FROM Employees”;  
  19.   
  20.                // Create instance of Northwind DataSetXSD  
  21.                NorthwindDataSet.EmployeeDataTable EmployeeDt = new NorthwindDataSet.EmployeeDataTable();  
  22.   
  23.                // Set a Data Commands  
  24.                SqlDataAdapter SqlDa = new SqlDataAdapter(SqlComm);  
  25.                SqlDa.Fill(EmployeeDt); // Fill Data in NorthwindDataSet Object.  
  26.   
  27.                return EmployeeDt;  
  28.   
  29.            }  
  30.            catch (Exception ex)  
  31.            {  
  32.                throw new Exception(ex.Message);  
  33.            }  
  34.        }  
  35.  
  36.        #endregion  

Step 15
Display Report in Report Viewer,it is look like this

  1. #region Display Report  
  2.         /// <summary>  
  3.         /// Display Report in Report Viewer  
  4.         /// </summary>  
  5.         private void DisplayReport()  
  6.         {  
  7.             try  
  8.             {  
  9.                 // Set a DataSource to the report  
  10.   
  11.                 // First Parameter – Report DataSet Name  
  12.                 // Second Parameter – DataSource Object i.e DataTable  
  13.                 ReportEmployee.LocalReport.DataSources.Add(new ReportDataSource(“EmployeeDataSet”, GetEmployeeData()));  
  14.   
  15.                 // Set A Report Embedded Resource  
  16.                 ReportEmployee.LocalReport.ReportEmbeddedResource = “SolRDLCReportWPF.NorthwindReport.rdlc”;  
  17.                 // OR Set Report Path  
  18.                 // Alternative: ReportEmployee.LocalReport.ReportPath = @”../../NorthwindReport.rdlc”;  
  19.   
  20.                 // Display Report  
  21.                 ReportEmployee.RefreshReport();  
  22.             }  
  23.             catch (Exception ex)  
  24.             {  
  25.                 throw new Exception(ex.Message);    
  26.             }  
  27.         }  
  28.  
  29.         #endregion  

Call DisplayReport function on window constructor,it is look like this

  1. public MainWindow()  
  2.        {  
  3.            InitializeComponent();  
  4.   
  5.            try  
  6.            {  
  7.                DisplayReport();   
  8.            }  
  9.            catch (Exception ex)  
  10.            {  
  11.                MessageBox.Show(ex.Message);    
  12.            }  
  13.        }  

Run the project.


Output



Click on image for better view


Full Code


1. XAML Code

  1. <Window x:Class=“SolRDLCReportWPF.MainWindow”  
  2.         xmlns=http://schemas.microsoft.com/winfx/2006/xaml/presentation&#8221;  
  3.         xmlns:x=http://schemas.microsoft.com/winfx/2006/xaml&#8221;  
  4.         xmlns:RV=“clr-namespace:Microsoft.Reporting.WinForms;assembly=Microsoft.ReportViewer.WinForms”  
  5.         Title=“MainWindow” Height=“350” Width=“525”>  
  6.     <Grid>  
  7.         <WindowsFormsHost x:Name=“WinFormHost”>  
  8.             <RV:ReportViewer x:Name=“ReportEmployee”></RV:ReportViewer>  
  9.         </WindowsFormsHost>  
  10.     </Grid>  
  11. </Window>  

2. Code Behind

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Windows;  
  6. using System.Windows.Controls;  
  7. using System.Windows.Data;  
  8. using System.Windows.Documents;  
  9. using System.Windows.Input;  
  10. using System.Windows.Media;  
  11. using System.Windows.Media.Imaging;  
  12. using System.Windows.Navigation;  
  13. using System.Windows.Shapes;  
  14. using System.Data;  
  15. using System.Data.SqlClient;  
  16. using Microsoft.Reporting.WinForms;  
  17.   
  18.   
  19. namespace SolRDLCReportWPF  
  20. {  
  21.     /// <summary>  
  22.     /// Interaction logic for MainWindow.xaml  
  23.     /// </summary>  
  24.     public partial class MainWindow : Window  
  25.     {  
  26.         public MainWindow()  
  27.         {  
  28.             InitializeComponent();  
  29.   
  30.             try  
  31.             {  
  32.                 DisplayReport();   
  33.             }  
  34.             catch (Exception ex)  
  35.             {  
  36.                 MessageBox.Show(ex.Message);    
  37.             }  
  38.         }  
  39.  
  40.         #region Bind Employee Data to DataSet Schema  
  41.         /// <summary>  
  42.         /// Get Employee data from Northwind database and bind in NorthwindDataSet  
  43.         /// </summary>  
  44.         /// <returns>DataTable</returns>  
  45.         private DataTable GetEmployeeData()  
  46.         {  
  47.             try  
  48.             {  
  49.                 // Open Sql Connection  
  50.                 SqlConnection SqlCon = new SqlConnection(@“Data Source=SHREE\SHREE;Initial Catalog=Northwind;Integrated Security=True”);  
  51.                 SqlCon.Open();  
  52.   
  53.                 // Create a Command  
  54.                 SqlCommand SqlComm = new SqlCommand();  
  55.                 SqlComm.Connection = SqlCon;  
  56.                 SqlComm.CommandType = CommandType.Text;  
  57.                 SqlComm.CommandText = “SELECT FirstName,LastName,BirthDate,Address,City,PostalCode,Country FROM Employees”;  
  58.   
  59.                 // Create instance of Northwind DataSetXSD  
  60.                 NorthwindDataSet.EmployeeDataTable EmployeeDt = new NorthwindDataSet.EmployeeDataTable();  
  61.   
  62.                 // Set a Data Commands  
  63.                 SqlDataAdapter SqlDa = new SqlDataAdapter(SqlComm);  
  64.                 SqlDa.Fill(EmployeeDt); // Fill Data in NorthwindDataSet Object.  
  65.   
  66.                 return EmployeeDt;  
  67.   
  68.             }  
  69.             catch (Exception ex)  
  70.             {  
  71.                 throw new Exception(ex.Message);  
  72.             }  
  73.         }  
  74.  
  75.         #endregion  
  76.  
  77.         #region Display Report  
  78.         /// <summary>  
  79.         /// Display Report in Report Viewer  
  80.         /// </summary>  
  81.         private void DisplayReport()  
  82.         {  
  83.             try  
  84.             {  
  85.                 // Set a DataSource to the report  
  86.   
  87.                 // First Parameter – Report DataSet Name  
  88.                 // Second Parameter – DataSource Object i.e DataTable  
  89.                 ReportEmployee.LocalReport.DataSources.Add(new ReportDataSource(“EmployeeDataSet”, GetEmployeeData()));  
  90.   
  91.                 // Set A Report Embedded Resource  
  92.                 ReportEmployee.LocalReport.ReportEmbeddedResource = “SolRDLCReportWPF.NorthwindReport.rdlc”;  
  93.                 // OR Set Report Path  
  94.                 // Alternative: ReportEmployee.LocalReport.ReportPath = @”../../NorthwindReport.rdlc”;  
  95.   
  96.                 // Display Report  
  97.                 ReportEmployee.RefreshReport();  
  98.             }  
  99.             catch (Exception ex)  
  100.             {  
  101.                 throw new Exception(ex.Message);    
  102.             }  
  103.         }  
  104.  
  105.         #endregion  
  106.     }  
  107. }  

 

 

Yorum bırakın