Monday, March 18, 2013

How to Create Report (RDLC) in WPF


Introduction

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":

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

and

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


Now the file will look like:

********************************************************

<UserControl x:Class="ListViewTutorial.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"
             Loaded="UserControl_Loaded">
    <Grid HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
        <WindowsFormsHost>
            <rv:ReportViewer x:Name="reportViewer" />
        </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 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()
        {
            InitializeComponent();
        }

        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)));

            try
            {
                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)];
                    dt.Rows.Add(dr);
                }

                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

                reportViewer.LocalReport.DataSources.Add(reportDataSource);
                reportViewer.RefreshReport();
            }
            catch (Exception)
            {
                throw;
            }
        }
    }
}

********************************************************

Step 5

Now the last step.


Open the MainWindow.xaml

Write the following XAML for this XAML file:


<Window x:Class="ListViewTutorial.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Window1" 
        Height="600" 
        Width="814"
        xmlns:report="clr-namespace:ListViewTutorial">
    <Grid>

        <report:ReportViewer ></report:ReportViewer>

    </Grid>
</Window>

Now Build the project and run it.


Post a Comment