Monday, March 18, 2013

How to Create Report (RDLC) in WPF


This article shows how to add a RDLC report in WPF.

Step 1

Create a WPF project

Step 2

Add a class in the project  named "Invoice.cs".

public class Invoice
        public String Sno { get; set; }
        public int OrderNumber { get; set; }
        public DateTime OrderDate { get; set; }
        public String CustomerName { get; set; }
        public String Address { get; set; }
        public Boolean Warranty { get; set; }

Step 3

Add an .rdlc file named "InvoiceReport.rdlc".

Now click on the "OK" button and a DataSet will be added for the InvoiceReport.rdlc report.

Step 4

Add a User Control in the "User Control" folder in the project named "ReportViewer.xaml"

This reportViewer user control will contain the RDLC report.

Now add two DLLs as references.

The first one is Microsoft.ReportViewer.WinForms.
The second one is WindowsFormsIntegration.

Now add the following line in your "ReportViewer.xaml":



       <rv:ReportViewer x:Name="reportViewer" />

Now the file will look like:


<UserControl x:Class="ListViewTutorial.ReportViewer"
    <Grid HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
            <rv:ReportViewer x:Name="reportViewer" />


On the UserControl_Loaded event write the following code.

We can bind the report with the database table from the SQL Server database but now I have generated a DataTable at run time with the same fields details Invoice.cs


using System;
using System.Data;
using System.Windows;
using System.Windows.Controls;
using Microsoft.Reporting.WinForms;

namespace ListViewTutorial
    /// <summary>
    /// Interaction logic for UserControl1.xaml
    /// </summary>
    public partial class ReportViewer : UserControl
        public ReportViewer()

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
            DataTable dt = new DataTable();
            dt.Columns.Add(new DataColumn("Sno", typeof(String)));
            dt.Columns.Add(new DataColumn("OrderNumber", typeof(int)));
            dt.Columns.Add(new DataColumn("OrderDate", typeof(DateTime)));
            dt.Columns.Add(new DataColumn("CustomerName", typeof(String)));
            dt.Columns.Add(new DataColumn("Address", typeof(String)));
            dt.Columns.Add(new DataColumn("Warranty", typeof(Boolean)));

                Random randNum = new Random();

                String[] states = { "Delhi", "UP", "MP", "Ap", "Panjap" };
                String[] names = { "Kaushik", "Amit", "Geeta", "Naveen", "Priyank", "Rahul", "Sumit", "Pushp", "Rohit", "Ravi", "Anand", "Suresh", "Rakesh", "Ajay", "Prabhat" };
                Boolean[] boolVals = { false, true, false, true, false, true, true, false };

                Invoice[] invoice = new Invoice[101];
                for (int index = 1; index < invoice.Length; index++)

                    DataRow dr = dt.NewRow();
                    dr["Sno"] = index.ToString();
                    dr["OrderNumber"] = randNum.Next(1, 1000);
                    dr["OrderDate"] = DateTime.Now.AddDays(randNum.Next(1, 30));
                    dr["CustomerName"] = names[randNum.Next(0, 14)].ToString();
                    dr["Address"] = String.Format("House Number  {0}, {1} ", randNum.Next(1, 1000), states[randNum.Next(0, states.Length - 1)].ToString());
                    dr["Warranty"] = boolVals[randNum.Next(0, boolVals.Length - 1)];

                ReportDataSource reportDataSource = new ReportDataSource();
                reportDataSource.Name = "InvoiceDataSet"; // Name of the DataSet we set in .rdlc
                reportDataSource.Value = dt;
                reportViewer.LocalReport.ReportPath = "D:\\My Projects\\ListViewTutorial\\ListViewTutorial\\InvoiceReport.rdlc"; // Path of the rdlc file

            catch (Exception)


Step 5

Now the last step.

Open the MainWindow.xaml

Write the following XAML for this XAML file:

<Window x:Class="ListViewTutorial.MainWindow"

        <report:ReportViewer ></report:ReportViewer>


Now Build the project and run it.

Post a Comment