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.

Create a new WPF

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

Class library

Add the following dll to your references.

Microsoft report

  1. Microsoft.ReportViewer.common
  2. Microsoft.ReportViewer.Winforms
  3. WindowFormsIntegration.

Create a new class

Create a new class called Person. cs and add the following codes to it.

namespace RDLCDemo  
{  
   public class Person  
    {  
        public int id {get; set;}  
        public string Name {get; set;}  
        public int Age {get; set;}  
  
    }  
}  

Rebuild the solution

Add a new Report Item and name it MainReport.rldc.

Report

Go to the MainReport.rdlc file.

  • Drag and drop a table from the toolbox; a dialog will show up demanding a Dataset. Name it "Person_DS".
  • Select "New" in the data source to add Data sources. A dialog will show up demanding 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.
    Dataset
    Object
    Select the data objects
    RDL demo

Right-click on the table and go to Tablix Properties, select the Person_DS as the data set, and click OK.

General

Enter the corresponding header for the table and map them with the correct properties, as shown below.

Table and map

Add this code to the personViewModel.cs.

using System.IO;  
namespace RDLCDemo  
{  
    public class PersonViewModel  
    {  
        private MainWindow _window;  
        private LocalReport _Report;  
        private ReportViewer _reportviewer;  
        public ViewModel(MainWindow window)  
        {  
            _window = window;  
            this. _reportviewer = window. _reportviewer;  
            Initialize ();  
        }  
private IEnumerable<Person> people = new List<Person>() { new Person { Name = "Gloria", id = 46, Age =12} ,  
        new Person {Name = "John", id = 1, Age =23},  
         new Person {Name = "Francis My Staff", id = 2, Age =12},  
        new Person {Name = "Ndu", id = 3, Age =32},  
        new Person {Name = "Murphy", id = 4, Age =22},  
        new Person {Name = "Mr Charles our boss", id = 5, Age =52}};       
      private void Initialize ()  
        {  
         _reportviewer.LocalReport.DataSources.Clear();  
       var rpds_model = new ReportDataSource () { Name = "Person_DS",Value = people };  
            _reportviewer.LocalReport.DataSources.Add(rpds_model);  
            _reportviewer.LocalReport.EnableExternalImages = true;  
   private static string _path = Path.GetDirectoryName(Path.GetDirectoryName(Path.GetDirectoryName(System.IO.Directory. GetCurrentDirectory())));  
   public static string ContentStart = _path + @"\ReportProject\MainPage.rdlc";  
            _reportviewer.LocalReport.ReportPath = ContentStart;  
            _reportviewer.SetDisplayMode(DisplayMode.PrintLayout);  
            _reportviewer.Refresh();  
            _reportviewer.RefreshReport();  
        }  
   }  
}  

This class initializes the Report Viewer. Set the correct data source and indicate the path of the MainReport.rdlc file.

Add the following code to your MainWindow.Xaml.

<Window x:Class="RDLCDemo.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:rdlcreport="clr-namespace:Microsoft.Reporting.WinForms;assembly=Microsoft.ReportViewer.WinForms"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <WindowsFormsHost Name="windowsFormsHost1" Grid.Row="0">
            <rdlcreport:ReportViewer x:Name="_reportviewer"/>
        </WindowsFormsHost>
    </Grid>
</Window>

Add this code to the MainWindow.Xaml. cs file.

namespace RDLCDemo
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            DataContext = new PersonViewModel(this);
        }
    }
}

Run the application and you will have a table like this in the reportView.

Run the application

Thank you.


Similar Articles