Search results
Suggest a FeaturePDF

Render subreport

You can display another report inside the body of a main report using the Report Viewer. The following steps helps you to customize the subreport properties such as data source, report path, and parameters.

  1. Add the sub report and main reports to the application Resources folder. In this tutorial, the already created reports are used. Refer to the Create RDL Report section or Create RDLC Report section section.

    Download the side-by-side-main-report.rdl, side-by-side-sub-report.rdl reports from here. You can add a report from the Syncfusion® installation location. For more information, refer to Samples and demos.

  • Set the ReportPath and ReportServiceUrl properties of the Report Viewer as in the following code snippet.

    The following code example demonstrates how to load a subreport in the Report Viewer at client side.

    @(Html.Bold().ReportViewer("viewer")
        .ReportPath("~/Resources/side-by-side-main-report.rdl")
        .ReportServiceUrl("/api/ReportViewer")
    )

Change subreport path

To change the subreport file path, set the Stream property of SubReportModel in the OnInitReportOptions method.

[NonAction]
public void OnInitReportOptions(ReportViewerOptions reportOption)
{
    string reportPath = System.Web.Hosting.HostingEnvironment.MapPath(@"~/Resources/" + reportOption.SubReportModel.ReportPath);

    if (reportOption.SubReportModel != null)
    {
        FileStream SubStream = new FileStream(reportPath, FileMode.Open, FileAccess.Read);
        reportOption.SubReportModel.Stream = SubStream;
    }
}

Set subreport parameter

You can change the parameter default values of a subreport in the OnReportLoaded method of the Web API Controller as given in the following code snippet.

[NonAction]
public void OnReportLoaded(ReportViewerOptions reportOption)
{
    if (reportOption.SubReportModel != null)
    {
        reportOption.SubReportModel.Parameters = new BoldReports.Web.ReportParameterInfoCollection();
        reportOption.SubReportModel.Parameters.Add(new BoldReports.Web.ReportParameterInfo()
        {
            Name = "SalesPersonID",
            Values = new List<string>() { "2" }
        });
    }
}

Modify subreport data source connection string

You can change the credential and connection information of the data sources used in the subreport using the SubReportModel in the OnInitReportOptions method.

[NonAction]
public void OnInitReportOptions(ReportViewerOptions reportOption)
{
    if (reportOption.SubReportModel != null)
    {
        reportOption.SubReportModel.DataSourceCredentials = new List<BoldReports.Web.DataSourceCredentials>();
        reportOption.SubReportModel.DataSourceCredentials.Add(new BoldReports.Web.DataSourceCredentials("<database>", "Data Source=<instancename>;Initial Catalog=<database>;user id=<username>;password=<password>"));
    }
}

Set subreport data source

You can bind local business object data source collection only for RDLC reports. To specify data source of a RDLC subreport, set the ReportDataSource property in the OnReportLoaded method.

The RDL report has the connection information in report definition itself, so no need to bind data source.

  1. Add the RDLC sub report and main reports to your application Resources folder. For more information, see Samples and demos.

  2. Set the ReportPath, ProcessingMode, and ReportServiceUrl properties of the Report Viewer as shown in following code snippet.

    @(Html.Bold().ReportViewer("viewer")
        .ProcessingMode(BoldReports.ReportViewerEnums.ProcessingMode.Local)
        .ReportServiceUrl("/api/SubreportDataSources")
        .ReportPath("~/Resources/product-list.rdlc")
    )
  3. Create a class and methods that returns business object data collection. Use the following code in the application Web API Service.

    public class ProductList
    {
        public string ProductName { get; set; }
        public string OrderId { get; set; }
        public double Price { get; set; }
        public string Category { get; set; }
        public string Ingredients { get; set; }
        public string ProductImage { get; set; }
    
        public static IList GetData()
        {
            List<ProductList> datas = new List<ProductList>();
            ProductList data = null;
            data = new ProductList()
            {
                ProductName = "Baked Chicken and Cheese",
                OrderId = "323B60",
                Price = 55,
                Category = "Non-Veg",
                Ingredients = "grilled chicken, corn and olives.",
                ProductImage = ""
            };
            datas.Add(data);
            data = new ProductList()
            {
                ProductName = "Chicken Delite",
                OrderId = "323B61",
                Price = 100,
                Category = "Non-Veg",
                Ingredients = "cheese, chicken chunks, onions & pineapple chunks.",
                ProductImage = ""
            };
            datas.Add(data);
            data = new ProductList()
            {
                ProductName = "Chicken Tikka",
                OrderId = "323B62",
                Price = 64,
                Category = "Non-Veg",
                Ingredients = "onions, grilled chicken, chicken salami & tomatoes.",
                ProductImage = ""
            };
            datas.Add(data);
    
            return datas;
        }
    }
  4. Bind the business object data values collection to the subreport by adding a new item to the DataSources as shown in the following code snippet.

    [NonAction]
    public void OnReportLoaded(ReportViewerOptions reportOption)
    {
        //Assigning the data source for 'Product List.rdlc'
        if (reportOption.SubReportModel != null)
        {
            reportOption.SubReportModel.DataSources = new BoldReports.Web.ReportDataSourceCollection();
            reportOption.SubReportModel.DataSources.Add(new BoldReports.Web.ReportDataSource { Name = "list", Value = ProductList.GetData() });
        }
    }

The data source name is case sensitive, it should be same as in the report definition.

Load subreport stream

To load subreport as stream, set the Stream property in the OnInitReportOptions method.

[NonAction]
public void OnInitReportOptions(ReportViewerOptions reportOption)
{
    if (reportOption.SubReportModel != null)
    {
        FileStream reportStream = new FileStream(System.Web.Hosting.HostingEnvironment.MapPath(@"~/Resources/" + reportOption.SubReportModel.ReportPath), FileMode.Open, FileAccess.Read);
        reportOption.SubReportModel.Stream = reportStream;
    }
}
[NonAction]
public void OnReportLoaded(ReportViewerOptions reportOption)
{
    if (reportOption.SubReportModel != null)
    {
        reportOption.SubReportModel.DataSources = new BoldReports.Web.ReportDataSourceCollection();
        reportOption.SubReportModel.DataSources.Add(new BoldReports.Web.ReportDataSource { Name = "list", Value = ProductList.GetData() });
    }
}