New to Telerik ReportingStart a free 30-day trial

How To Programmatically Create a Master-Detail Report Using SubReport Item

Environment

Product Version
ProductProgress® Telerik® Reporting

Description

Using the SubReport Item with Parameters in a Master-Detail Report is described in the KB article Create Master-Detail Reports by Using SubReport Items.

You may do this by Embedding the Reporting Engine as well.

Solution

In this case, the logic requires creating two InstanceReportSource objects and a Report Object from the Main Report.(cs|vb) class. Query the Main Report Object for the SubReport Item. Then set the Report Source of the SubReport Item to the SubReport Instance Report Source. See the below code snippets for more details.

C#
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Security;
using System.Text;
using System.Threading.Tasks;
using Telerik.Reporting;
using Telerik.Reporting.Processing;

static class Program
{
	public static void Main()
	{
		Console.WriteLine("Enter Number: 1, 2 or 3");
		object Input = Console.ReadLine();

		ReportProcessor ReportProcessor = new ReportProcessor();
		Hashtable DeviceInfo = new Hashtable();

		// Create MainReport InstanceReportSource
		InstanceReportSource MainInstanceReportSource = new InstanceReportSource();

		// Create SubReport InstanceReportSource
		InstanceReportSource SubInstanceReportSource = new InstanceReportSource()
		{
			ReportDocument = new SubReport() // Set ReportDocument of SubInstanceReportSource
		};

		// Set Parameters of SubInstanceReportSource
		SubInstanceReportSource.Parameters.Add("PersonId", Input);

		// Create MainReport Object
		MainReport MainReportObject = new MainReport();

		// Find SubReport Report Item of Main Report
		Telerik.Reporting.SubReport SubReportSource = MainReportObject.Items.Find("SubReport1", true)(0);

		// Set Report Source of SubReport in MainReport
		SubReportSource.ReportSource = SubInstanceReportSource;

		// Set the ReportDocument of the MainInstanceReportSource
		MainInstanceReportSource.ReportDocument = MainReportObject;

		// Set Parameters of MainInstanceReportSource
		MainInstanceReportSource.Parameters.Add("PersonId", Input);

		// Continue with Embedding the Report Engine in Application
		RenderingResult PdfRendering = ReportProcessor.RenderReport("PDF", MainInstanceReportSource, DeviceInfo);

		string FileName = PdfRendering.DocumentName + "." + PdfRendering.Extension;

		// Saves the Report in Temp Folder C:\Users\[USER]\AppData\Local\Temp
		string Path = System.IO.Path.GetTempPath();
		string FilePath = System.IO.Path.Combine(Path, FileName);

		// Writes File To Disk
		using (FileStream Stream = new FileStream(FilePath, FileMode.Create))
		{
			Stream.Write(PdfRendering.DocumentBytes, 0, PdfRendering.DocumentBytes.Length);
		}

		Console.WriteLine($"File Created: {FilePath}");

		Console.WriteLine("Press Enter to Exit");

		Console.ReadKey();
	}
}

See Also