In this article I am going explain about sub report control of RDLC and how can we use sub report in RDLC and report viewer by taking a real life example in an asp.net application.
I have taken an example of Orders and its Line items. Our report will something look like this:
In order to achieve above screenshot in a browser, you can follow the steps given below:
1) Open Microsoft Visual Studio
2) Select Asp.Net Empty Web Application.
3) Add a new item (Report.aspx)
4) Drag and Drop ReportViewer control and ScriptManager from the toolbox.
Add the following Object and Business class in order to specify the data for reportviewer:
Create a Report folder in the root directory of your application and add two report (.rdlc) files in the same folder Report.rdlc and Report1.rdlc
Also add a Dataset in Report folder and create these two DataTable in the same dataset.
1) Order (OrderNo: Int64, OrderDate: DateTime, Customer: String)
2) Line (OrderNo: Int64, LineNo: Int32, Product: String, UnitPrice: Double, Quantity: Int32, VAT: Double, Discount: Double, Total: Double)
1) Now add an Order DataTable in Report.rdlc file and also drag and drop Table control from Toolbox and set the columns of DataTable in Table columns.
2) Remove the header row from the table control.
3) Add a parameter Total of type float in the Parameters folder.
4) Drag and Drop Textbox control and set the Total parameter in the textbox.
5) Right click on Table row and select Insert Row à select Inside Group – Below.
6) Select all the columns of added row and right click and choose Merge Cells option.
7) Now Drag and Drop Subreport control from toolbox in this row.
8) Set the properties of Subreport.
a. ReportName: Report1
b. Name: LineInfo
c. Hidden: True
d. ToggleItem: OrderNo
9) Right Click on Subreport and add a parameter (OrderNo) to display the OrderNo wise LineInfo.
I also highlighted the configured part in the above screenshot for your reference. Please make sure all the steps should be properly configured.
1) Now add a Line DataTable in Report1.rdlc file and also drag and drop Table control from Toolbox and set the columns of DataTable in Table columns.
2) Select Row of the Table à Select Details from Row Groups give at the bottom of the page àChoose Add Total option à Choose After.
3) Remove all sum columns except Sum(Total) and merge all the cells in this row.
Our configuration part is completed now. Now we will move towards our server side code where we dynamically set the data in reportviewer control and its subreport.
Open Report.aspx.cs and add the following two functions:
Now all we set to see the output in browser window. Run an application and browse Report.aspx. I hope the output should be fine and we can the list orders and line items y expanding the rows.
Thanks for reading this article. I think this will helps a lot. You can ask any question and can also provide feedback for this article by using the comment box given below.
In order to see reportviewer basic you can visit this article Using ReportViewer in WinForms C#