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
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
Finally it's look like this
Step 7
Wrap the ReportViewer control in a WindowsFormsHostelement,it is look like this
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
Step 15
Display Report in Report Viewer,it is look like this
Call DisplayReport function on window constructor,it is look like this
Run the project.
Output
Click on image for better view
Full Code
1. XAML Code
2. Code Behind
Download
Download Source Code
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
Nice article kishor.
ReplyDeleteThank you so much Kishor... :)
ReplyDeletebinil@msn.com
Thank you for this great article.
ReplyDeleteAndreea
Most Welcome Binil and Andreea
ReplyDeleteThanks a lot for giving nice document
ReplyDeleteMost Welcome Anwar.......
ReplyDeleteIt worked for me...
ReplyDeleteThanks Brother........
DeleteVery very nice and Easy to understand..I love this <3
ReplyDeleteThanks a lot broo :)
Most Welcome Brother.
DeleteThanks for you like my article.
Do u know how to print a report without preview ?
ReplyDeleteCheck the following Link
Deletehttp://msdn.microsoft.com/en-us/library/ms252091.aspx
Good article !! One should appreciate ur effort ..Thanks !!
ReplyDeleteThanks.....
Deletereally superb
ReplyDeleteThanks Satyanand
DeleteThanks, helped a lot.
ReplyDeletenice and useful article dear
ReplyDeleteNice Work Brother ... Keep on Goin :-)
ReplyDeleteThanks SriRam
DeleteVery helpful post brother, many many thanks, how can i add some parameters like year, search by date, employee ?
ReplyDeleteBro it's telling "A data source instance has not been supplied for the data source 'DataSet1' "
ReplyDeleteThanks
ReplyDeleteMost Welcome Prabu
DeleteSimple and easy to understand :) thank you kishore
ReplyDeleteMost Welcome Tina.....
DeleteJor dar
ReplyDeleteસૌથી વધુ સ્વાગત
Deletegreat sir
ReplyDeleteThanks Vijay.......
DeleteThanx a lot
ReplyDeleteKishor Thanks for help I need it in MVVM way can it be possible
ReplyDeleteağrı
ReplyDeletevan
elazığ
adıyaman
bingöl
SPA
yalova
ReplyDeleteyozgat
elazığ
van
sakarya
XDZT1Y
görüntülü show
ReplyDeleteücretlishow
0YHPL
https://titandijital.com.tr/
ReplyDeletemersin parça eşya taşıma
osmaniye parça eşya taşıma
kırklareli parça eşya taşıma
tokat parça eşya taşıma
DJDH8X
ankara parça eşya taşıma
ReplyDeletetakipçi satın al
antalya rent a car
antalya rent a car
ankara parça eşya taşıma
2KZNW
sakarya evden eve nakliyat
ReplyDeleteosmaniye evden eve nakliyat
aksaray evden eve nakliyat
çanakkale evden eve nakliyat
zonguldak evden eve nakliyat
MNW
E2DD8
ReplyDeleteYalova Parça Eşya Taşıma
Samsun Şehirler Arası Nakliyat
Düzce Lojistik
Mamak Parke Ustası
Yozgat Parça Eşya Taşıma
Kripto Para Nedir
Afyon Şehir İçi Nakliyat
Silivri Boya Ustası
Bitlis Şehirler Arası Nakliyat
8218D
ReplyDeleteBurdur Parça Eşya Taşıma
Giresun Şehir İçi Nakliyat
Antalya Rent A Car
Kars Şehir İçi Nakliyat
Bitlis Evden Eve Nakliyat
Ordu Evden Eve Nakliyat
Sinop Parça Eşya Taşıma
Eskişehir Evden Eve Nakliyat
Kars Lojistik
4FFE1
ReplyDeletebinance %20 komisyon indirimi
1AA67
ReplyDeletereferans
563EF
ReplyDeletekırıkkale yabancı görüntülü sohbet uygulamaları
artvin canli sohbet bedava
Hakkari Kadınlarla Görüntülü Sohbet
canli sohbet bedava
telefonda görüntülü sohbet
Elazığ Sohbet Muhabbet
kocaeli kadınlarla sohbet et
Mobil Sohbet Siteleri
tunceli rastgele görüntülü sohbet ücretsiz
78250
ReplyDeleterize rastgele görüntülü sohbet
sesli sohbet
kayseri sesli sohbet sesli chat
manisa yabancı sohbet
Antep En İyi Sesli Sohbet Uygulamaları
Nevşehir Kadınlarla Rastgele Sohbet
isparta canlı sohbet
en iyi görüntülü sohbet uygulaması
osmaniye görüntülü sohbet yabancı
1E6A1
ReplyDeleteBinance Referans Kodu
Dxy Coin Hangi Borsada
Soundcloud Takipçi Hilesi
Bitcoin Kazanma
Mexc Borsası Güvenilir mi
Bitcoin Nasıl Alınır
Yeni Çıkacak Coin Nasıl Alınır
Aion Coin Hangi Borsada
Dxgm Coin Hangi Borsada
شركة شفط بيارات Su21B05z0T
ReplyDelete