C# Crystal Reports - on demand sub reports

The following program shows how to create a Crystal Report On Demand Subreport in C#.

All C# Crystal Reports Tutorial in this website is based on the following database - crystaldb. So before you begin this section , please take a look at the database structure of crystaldb - Click Here C# crystaldb

If you are new to Crystal Reports and do not know how to create Crystal Reports from C# , please take a look at the section step by step tutorial for creating a Crystal Reports from C#.

In the previous section of C# Crystal Reports sub-reports is described how to insert a subreport in Crystal Reports . In the previous section the sub report is displayed directly under each row details of main report .

The subsequent section provides a comprehensive guide on how to incorporate an on-demand subreport feature within the main Crystal Report. This approach enables the display of the subreport when the user clicks on a hyperlink embedded within the main report. In this scenario, the subreport solely contains a link back to the main report. Thus, when the user activates the link, the subreport is dynamically presented, creating an on-demand report experience.

csharp-crystal-report-subreport-link

This section is the continuation of the previous section C# Crystal Reports sub-reports. Here we are creating a Sub report in Crystal Report and make a link in the main Crystal Reports for on-demand subreport.

Select the subreport object in the Crystal Reports and right click , then select Format Object .

csharp-crystal-report-subreport-format

Then you will get the Format Editor . Select Sub report tab from Format Editor , you can find there a check box - On-demand Subreport . You have to select that check box , then the sub report become as a link in your main Crystal Reports. If you want to change the title , you can change it in subreport name textbox. Finally click OK button.

csharp-crystal-report-subreport-editor

Now the designing part is over and the next step is to call the Crystal Reports in CSharp and view it in Crystal Reports Viewer control .

Now the designing part is over and the next step is to call the Crystal Reports in C# and view it in Crystal Reports Viewer control .

You have to include CrystalDecisions.CrystalReports.Engine in your C# Source Code.

using CrystalDecisions.CrystalReports.Engine; using CrystalDecisions.Shared;

Copy and paste the following source code and run your C# project.

Full Source C#
using System; using System.Windows.Forms; using CrystalDecisions.CrystalReports.Engine; using CrystalDecisions.Shared; namespace WindowsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { ReportDocument cryRpt = new ReportDocument(); cryRpt.Load(PUT CRYSTAL REPORT PATH HERE\\CrystalReport1.rpt"); crystalReportViewer1.ReportSource = cryRpt; crystalReportViewer1.Refresh(); } } }