How to Create Report (RDLC) in WPF

Introduction

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

Suppose I have been assigned the task of generating a RDLC report in WPF.

That contains ID, Name, City, and Order Amount.

So I will show you how to generate a RDLC report in WPF.

It is very simple; believe me it is very simple. So let's start.

Step 1

Create a WPF project; I named it "MonsterBusinessInc".

RDLCWPF1.jpg


Step 2

Add a WPF Button, the click event of this button will show you the report; also add the click event for this button.

RDLCWPF2.jpg

private void btnClickMe_Click(object sender, RoutedEventArgs e)

   {

 

   }

Step 3

Add a class in the project under the Entitles folder named "Customer.cs".

Write the code like below:

namespace MonsterBusinessInc.Entities

{

    public class Customer

    {

        public int ID { get; set; }

        public string Name { get; set; }

        public string City { get; set; }

        public int OrderAmount { get; set; }

 

    }

}

Build the project after doing it.

Step 4

Add an .rdlc file named "CustomerReport.rdlc".

RDLCWPF3.jpg


Now we have to add the dataset for this report that will be filled at run time when we display the report; i.e. ID, Name, City etc.

RDLCWPF4.jpg

Click the DataSet on the Report Data window and the DataSet Properties window will open. Now we must set the properties.

RDLCWPF5.jpg

Set the name to "CustomerDataSet". Now click on the "New" Button; the DataSource Window will open.

RDLCWPF6.jpg

Select Object and click the "Next" Button.

RDLCWPF7.jpg

Now select the Customer Class under Entities that we created earlier and click the "Finish" button.

RDLCWPF8.jpg

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

And you can see it in the Report Data Window.

RDLCWPF9.jpg

Now right-click on the blank white area and select the table.

RDLCWPF10.jpg

Now you will have a blank table in the surface.

Click in the blank field; set the filed name. You also can edit the header for the field.

RDLCWPF11.jpg


RDLCWPF12.jpg

Now we have a .rdlc report ready to display records after supplying the datasource.

Step 5

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

RDLCWPF13.jpg

This reportViewer user control will contain the RDLC report.

Now add two DLLs as references.

The first one is Microsoft.ReportViewer.WinForms .

RDLCWPF14.jpg

The second one is WindowsFormsIntegration.

RDLCWPF15.jpg

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

xmlns:rv="clr-namespace:Microsoft.Reporting.WinForms;assembly=Microsoft.ReportViewer.WinForms"

 

and

 

<WindowsFormsHost>

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

        </WindowsFormsHost>

Now the file will look like:
 

<UserControl x:Class="MonsterBusinessInc.User_Control.ReportViewer"

             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"

             xmlns:rv="clr-namespace:Microsoft.Reporting.WinForms;assembly=Microsoft.ReportViewer.WinForms"

             mc:Ignorable="d"

             d:DesignHeight="600" d:DesignWidth="800" Loaded="UserControl_Loaded"

 >

    <Grid HorizontalAlignment="Stretch" VerticalAlignment="Stretch">

        <WindowsFormsHost>

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

        </WindowsFormsHost>

    </Grid>

</UserControl>

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 Customer.cs under the Entities folder.
 

using System.Data;

using Microsoft.Reporting.WinForms;

namespace MonsterBusinessInc.User_Control

{

    /// <summary>

    /// Interaction logic for ReportViewer.xaml

    /// </summary>

    public partial class ReportViewer : UserControl

    {

        public ReportViewer()

        {

            InitializeComponent();

        }

 

   private void UserControl_Loaded(object sender, RoutedEventArgs e)

        {

            DataTable dt = new DataTable();

            dt.Columns.Add(new DataColumn ("ID",typeof(int)));

            dt.Columns.Add(new DataColumn("Name", typeof(string)));

            dt.Columns.Add(new DataColumn("City", typeof(string)));

            dt.Columns.Add(new DataColumn("OrderAmount", typeof(int)));

 

            DataRow dr = dt.NewRow();

            dr["ID"] = 1;

            dr["Name"] = "CK Nitin";

            dr["City"] = "New York";

            dr["OrderAmount"] = 100;

            dt.Rows.Add(dr);

                       

 

            ReportDataSource reportDataSource = new ReportDataSource();

            reportDataSource.Name = "CustomerReport"; // Name of the DataSet we set in .rdlc

            reportDataSource.Value = dt;

            reportViewer.LocalReport.ReportPath = "D:\\MonsterBusinessInc \\Report\\CustomerReport.rdlc"; // Path of the rdlc file

     

            reportViewer.LocalReport.DataSources.Add(reportDataSource);

            reportViewer.RefreshReport();

        }

 

private void reportViewer_RenderingComplete(object sender, Microsoft.Reporting.WinForms.RenderingCompleteEventArgs e)

        {

 

        }

    }

}

Step 7

Now add a window named "CustomerReport.xaml".

Write the following XAML for this XAML file:
 

<Window x:Class="Reporting.CustomerReport"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        Title="CustomerReport" Height="600" Width="300"

        xmlns:report="clr-namespace:Reporting.User_Control">

    <Grid>

        <report:ReportViewer ></report:ReportViewer>

       

    </Grid>

</Window>

RDLCWPF3.jpg

Step 8

Now the last step.

Open the MainWindows.xaml

Write the following code in the button click:

CustomerReport rpt = new CustomerReport();

            rpt.ShowDialog();

 

private void btnClickMe_Click(object sender, RoutedEventArgs e)

        {

            CustomerReport rpt = new CustomerReport();

            rpt.ShowDialog();

           

        }

Now Build the project and run it.

Here is the output.

OUTPUT

RDLCWPF17.jpg
 

Up Next
    Ebook Download
    View all
    Learn
    View all
    Founded in 2012 by Red Ant Corp, Red Ant Corp & Associates Consulting (Red Ant Corp).