RDLC Report Using WPF And C#

Initially, RDLC was used in just web applications but presently, it can be used in both, web and desktop applications. This tutorial assumes we already have a basic understanding of C# and WPF, so I will just dive into the demo.

Make sure you have the Microsoft RDLC Reporting Designer installed in Visual Studio.

  1. Create a new WPF project and name it "RDLCDemo".

    RDLC Report Using WPF And C#

    Add the following dll to your references.
    1. Microsoft.ReportViewer.common  
    2. Microsoft.ReportViewer.Winforms  
    3. WindowFormsIntegration.  
    RDLC Report Using WPF And C#
  1. Create a new class called Pesron.cs and add the following codes to it.
    1. namespace RDLCDemo  
    2. {  
    3.    public class Person  
    4.     {  
    5.         public int id {getset;}  
    6.         public string Name {getset;}  
    7.         public int Age {getset;}  
    8.   
    9.     }  
    10. }  
  1. Rebuild the solution.
  1. Add a new Report Item and name it MainReport.rldc.

    RDLC Report Using WPF And C#
Go to the MainReport.rdlc file.
  • Drag and drop a table from the toolbox; a dialog will show up demanding for a Dataset. Name it as "Person_DS".
  • Select "New" in the data source to add Data sources. A dialog will show up demanding for a data source.
  • The Data source can be directly from a database but in our scenario, we are getting the data from the person class object. So, select the object as the data source.
  • A list of available models will be provided; check the Person Checkbox since that will be the model we are using.
RDLC Report Using WPF And C#
 
RDLC Report Using WPF And C#
RDLC Report Using WPF And C#
RDLC Report Using WPF And C#
  1. Right-click on the table and go to Tablix Properties, select the Person_DS as the data set, and click OK.

    RDLC Report Using WPF And C#
  1. Enter the corresponding header for the table and map them with the correct properties, as shown below.

    RDLC Report Using WPF And C#
  1. Add this code to the personViewModel.cs.
    1. using System.IO;  
    2. namespace RDLCDemo  
    3. {  
    4.     public class PersonViewModel  
    5.     {  
    6.   
    7.         private MainWindow _window;  
    8.         private LocalReport _Report;  
    9.         private ReportViewer _reportviewer;  
    10.         public ViewModel(MainWindow window)  
    11.         {  
    12.             _window = window;  
    13.             this. _reportviewer = window. _reportviewer;  
    14.             Initialize ();  
    15.   
    16.         }  
    17.   
    18. private IEnumerable<Person> people = new List<Person>() { new Person { Name = "Gloria", id = 46, Age =12} ,  
    19.         new Person {Name = "John", id = 1, Age =23},  
    20.          new Person {Name = "Francis My Staff", id = 2, Age =12},  
    21.         new Person {Name = "Ndu", id = 3, Age =32},  
    22.         new Person {Name = "Murphy", id = 4, Age =22},  
    23.         new Person {Name = "Mr Charles our boss", id = 5, Age =52}};       
    24.       private void Initialize ()  
    25.         {  
    26.          _reportviewer.LocalReport.DataSources.Clear();  
    27.        var rpds_model = new ReportDataSource () { Name = "Person_DS",Value = people };  
    28.             _reportviewer.LocalReport.DataSources.Add(rpds_model);  
    29.             _reportviewer.LocalReport.EnableExternalImages = true;  
    30.    private static string _path = Path.GetDirectoryName(Path.GetDirectoryName(Path.GetDirectoryName(System.IO.Directory. GetCurrentDirectory())));  
    31.    public static string ContentStart = _path + @"\ReportProject\MainPage.rdlc";  
    32.   
    33.             _reportviewer.LocalReport.ReportPath = ContentStart;  
    34.             _reportviewer.SetDisplayMode(DisplayMode.PrintLayout);  
    35.             _reportviewer.Refresh();  
    36.             _reportviewer.RefreshReport();  
    37.         }  
    38.    }  
    39. }  
    This class basically initializes the Report Viewer. Set the correct data source and indicate the path of the MainReport.rdlc file.
  1. Add the following code to your MainWindow.Xaml.
    1. <Window x:Class="RDLCDemo.MainWindow"  
    2.         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
    3.         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"  
    4.         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
    5.         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"  
    6.         xmlns:rdlcreport="clr-namespace:Microsoft.Reporting.WinForms;assembly=Microsoft.ReportViewer.WinForms"  
    7.         mc:Ignorable="d"  
    8.         Title="MainWindow" Height="450" Width="800">  
    9.     <Grid>  
    10.         <WindowsFormsHost Name="windowsFormsHost1" Grid.Row="0" >  
    11.             <rdlcreport:ReportViewer x:Name="_reportviewer" />  
    12.         </WindowsFormsHost>  
    13.     </Grid>  
    14. </Window>  
  1. Add this code to the MainWindow.Xaml.cs file.
    1. namespace RDLCDemo  
    2. {  
    3.     /// <summary>  
    4.     /// Interaction logic for MainWindow.xaml  
    5.     /// </summary>  
    6.     public partial class MainWindow : Window  
    7.     {  
    8.         public MainWindow()  
    9.         {  
    10.             InitializeComponent();  
    11.             DataContext = new PersonViewModel(this);  
    12.         }  
    13.     }  
    14. }  
  1. Run the application and you will have a table like this in the reportView.

    RDLC Report Using WPF And C#

Thank you.