C# Crystal Reports - sub reports
The following progrm describes how to create a sub report within a Crystal Report 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 Crystal Reports, a subreport refers to a report embedded within another Crystal Report. It allows us to present supplementary information regarding a data field within a row of the Crystal Reports. There are two distinct approaches to displaying subreports:
- Directly beneath the main row details: The subreport is exhibited directly beneath the primary row details, providing immediate access to the additional information.
- On-demand subreports in C# Crystal Reports: In this method, a hyperlink is placed below the respective row field. When the user clicks the hyperlink, the subreport is dynamically displayed, allowing users to access the relevant details as per their requirements.
In this section we are going to generate a sub-report under the main row details. That is for each row there is a subreoprt for the details.
Here we are going create an order report based on three tables in the database and show a subreoprt for each row field of Product Name. Here we are using our earlier program C# Crystal Reports from multiple tables to show the main Crystal Reports Data , so refer C# Crystal Reports from multiple tables , before start this section .
Create a Crystal Reports using three tables and select customername , date , product and qty . It will explain in detail the previous section C# Crystal Reports from multiple tables.
Next step is to create a sub report inside the main report. Here we are showing the Product details in each row of the specified product in the main row.
After create the main report , right click on Crystal Reports designer window and select Insert-Subreport.
To add a subreport in Crystal Reports, follow these steps:
- Obtain the subreport object by selecting it from the appropriate location, then drag and drop it into the designer window. Place it below the fields within the details tab.
- After releasing the mouse, a dialogue box will prompt you to enter a report name. Provide a suitable name and click the "Report Wizard" button.
- The wizard will present a table selection screen. Choose the desired table from the available options. In this case, select the "Product Table" and click "Next".
- The subsequent screen will display the selected table. From there, select the specific fields you wish to include in the subreport. Once done, click "Finish".
- Return to the main screen of the subreport and navigate to the "Link" tab. This tab enables the establishment of a relationship between the main report and the subreport.
- To link the subreport with the main report, select the "Product.Product_name" from the list of available fields. This creates the necessary connection between the respective reports.
Accept the other settings as it is in the screen and click ok. Now you can see the sub report object in the screen , if you want to re-arrange subreport design , double click on subreport object then you can design subreport.
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 .
Select the default form (Form1.cs) you created in C# and drag a button and a CrystalReportViewer control to your form .
You have to include CrystalDecisions.CrystalReports.Engine in your C# Source Code.
Copy and paste the following source code and run your C# project.Full Source C#