RDLC SubReport Using C# And WPF

Subreports help in splitting a report into multiple parts and allow us to control the amount of information a page can contain.

In my previous post, I wrote about creating an RDLC report using C# and WPF. You can check it out through this link.

In this article, I will be creating a project that uses SubReport to display the information of employees based on the department of each employee.

  1. Create a new WPF project called SubReportDemo.

    RDLC SubReport Using C# And WPF
  1. Create a new class called Employee and add the following code to it.
    1. namespace SubreportDemo  
    2.   {  
    3.      public  class Employee  
    4.      {  
    5.         public int ID { getset; }  
    6.         public string Name { getset; }  
    7.         public string Email { getset; }  
    8.         public string Gender { getset; }  
    9.     }  
    10. }  
  1. Create another class called Department and add the following code to it.
    1. namespace SubreportDemo  
    2. {  
    3.     public class Department  
    4.     {  
    5.         public string Name { getset; }  
    6.         public int ID { getset; }  
    7.     }  
    8. }  
  1. Rebuild the project.
  2. Add a new report item and name it MainReport.RDLC. Drag and drop a table into the report and set the data source to the Department model. Name the dataset "Department_DS".

    RDLC SubReport Using C# And WPF
  1. Design the table as shown below. Map the Name property to the Department column and add a SubReport to the Employees column.

    RDLC SubReport Using C# And WPF
  1. Right-click on the Subreport and select Properties. Set the Subreport name as EmployeeDetails, as shown below.

    RDLC SubReport Using C# And WPF
     
    Go to Parameters and set the Name field as ID and select the ID from the dropdown in the Value field.
     
    RDLC SubReport Using C# And WPF
    RDLC SubReport Using C# And WPF
  1. Add another report file and name it Rdlc. This is the file that will be used as the Subreport. Always make sure that the name of the Subreport is same as the FileName of the Subreport without the rdlc extension.
  1. Add a table to the rdlc file, select the Employee Model as Datasource, and set the name of the dataset as Employee_DS.

    Map the columns of the table to the corresponding properties of the dataset.

    RDLC SubReport Using C# And WPF
  1. Right-click on Parameters from the "Report Data Tool" window, add a parameter, and set its name as ID.

    The name should always be the same as the name of parameter given to the Subreport initially.

    ID is an int type so set the Datatype as Integer.

    RDLC SubReport Using C# And WPF

    RDLC SubReport Using C# And WPF
  1. Create a class called SubReportDemoViewModel.cs and add the below code to it.
    1. using Microsoft.Reporting.WinForms;  
    2. using System;  
    3. using System.Collections.Generic;  
    4. using System.IO;  
    5. namespace SubreportDemo  
    6. {  
    7.     public class SubReportDemoViewModel  
    8.     {  
    9.         private MainWindow _mainWindow;  
    10.         private ReportViewer _reportViewer;  
    11.         public SubReportDemoViewModel(MainWindow window)  
    12.         {  
    13.             _mainWindow = window;  
    14.             _reportViewer = window.reportViewer;  
    15.             Initialize();  
    16.   
    17.         }  
    18.   
    19.         private IEnumerable<Department> departments = new List<Department>()  
    20.        {  
    21.            new Department() {ID = 1, Name = "Applied Mathematics" },  
    22.            new Department() {ID = 2, Name = "Software" },  
    23.            new Department() {ID = 3, Name = "Machine Learning" },  
    24.            new Department() {ID = 4, Name = "Petroleum Engineering" },  
    25.        };  
    26.         private void Initialize()  
    27.         {  
    28.             _reportViewer.LocalReport.DataSources.Clear();  
    29.             var departmentsModels = new ReportDataSource() { Name = "Department_DS", Value = departments };  
    30.             _reportViewer.LocalReport.DataSources.Add(departmentsModels);  
    31.             var path = Path.GetDirectoryName(Path.GetDirectoryName  
    32. (Path.GetDirectoryName(Directory.GetCurrentDirectory())));  
    33.             var MainPage = path + @"\subreportDemo\MainReport.rdlc";  
    34.             _reportViewer.LocalReport.ReportPath = MainPage;  
    35.             _reportViewer.LocalReport.SubreportProcessing += LocalReport_SubreportProcessing;  
    36.             _reportViewer.LocalReport.EnableExternalImages = true;  
    37.             _reportViewer.SetDisplayMode(DisplayMode.PrintLayout);  
    38.             _reportViewer.Refresh();  
    39.             _reportViewer.RefreshReport();  
    40.   
    41.         }  
    42. private List<Employee> Employees = new List<Employee>()  
    43. {  
    44. new Employee() {ID = 1, Name = "Sam", Gender ="male", Email="[email protected]"},  
    45. new Employee() {ID = 1, Name = "Ella",Gender ="female", Email="[email protected]" },  
    46. new Employee() {ID = 1, Name = "TG",Gender ="male", Email ="[email protected]" },  
    47. new Employee() {ID = 1, Name = "Favor",Gender ="female", Email="[email protected]" },  
    48.         new Employee() {ID = 2,Name = "Micheal",Gender ="male",Email ="[email protected]" },  
    49.         new Employee() {ID = 2, Name = "Joe",Gender ="male", Email ="[email protected]" },  
    50. new Employee() {ID = 2, Name="Maintain",Gender ="female",Email ="[email protected]" },  
    51. new Employee() {ID = 3, Name = "Akeem",Gender ="male", Email ="[email protected]" },  
    52. new Employee() {ID = 3, Name = "Boye",Gender ="male", Email ="[email protected]" },  
    53. new Employee() {ID = 4, Name ="Chioma",Gender ="female",Email ="[email protected]" },  
    54. new Employee() {ID = 4, Name = "Ofure",Gender ="female", Email ="[email protected]" },  
    55. new Employee() {ID = 4, Name = "Hart",Gender ="male",Email ="[email protected]" }  
    56. };  
    57.         private void LocalReport_SubreportProcessing(object sender, SubreportProcessingEventArgs e)  
    58.         {  
    59.             var ID = Convert.ToInt32(e.Parameters[0].Values[0]);  
    60.             var employeegroup = Employees.FindAll(x => x.ID == ID);  
    61.             if (e.ReportPath == "EmployeeDetails")  
    62.             {  
    63.                 var employeeDetails = new ReportDataSource() { Name = "Employee_DS", Value = employeegroup };  
    64.                 e.DataSources.Add(employeeDetails);  
    65.             }  
    66.         }  
    67.     }  
    68. }  
    The reportViewer.LocalReport.SubreportProcessing event is triggered whenever a Subreport is found on a page and the method that subscribes to the event checks the Subreport path and uses ID which was set as the parameter to filter the information to display.
  1. Add this code the the mainWindow.xaml file.
    1. <Window x:Class="SubreportDemo.MainWindow"  
    2.         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
    3.         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
    4.         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"  
    5.         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"  
    6.         xmlns:local="clr-namespace:SubreportDemo"  
    7.         mc:Ignorable="d"  
    8.         xmlns:rdlc="clr-    namespace:Microsoft.Reporting.WinForms;assembly=Microsoft.ReportViewer.WinForms"  
    9.         Title="MainWindow" Height="350" Width="525">  
    10.     <Grid>  
    11.         <WindowsFormsHost >  
    12.             <rdlc:ReportViewer x:Name="reportViewer"/>  
    13.         </WindowsFormsHost>  
    14.     </Grid>  
    15. </Window>  
  1. Finally, add these to the MainWindow.xaml.cs file.
    1. using System.Windows;  
    2.   
    3. namespace SubreportDemo  
    4. {  
    5.     /// <summary>  
    6.     /// Interaction logic for MainWindow.xaml  
    7.     /// </summary>  
    8.     public partial class MainWindow : Window  
    9.     {  
    10.         public MainWindow()  
    11.         {  
    12.             InitializeComponent();  
    13.             DataContext = new SubReportDemoViewModel(this);  
    14.         }  
    15.     }  
    16. }  
  1. Run the application
    RDLC SubReport Using C# And WPF


Recommended Free Ebook
Similar Articles