Continue to learn about "parameter transfer" and "master-slave Report" in the rdlc report"
1. Create a dataset firstSuch:
2. Create a report rptdept. rdlc to display the t_dpet data of the department.
3. embedded in default. aspx and written in default. aspx. CSCode
Using system; using system. data; using Microsoft. reporting. webforms; namespace reportsample {public partial class default: system. web. UI. page {protected void page_load (Object sender, eventargs e) {If (! Ispostback) {This. reportviewer1.localreport. reportpath = "rptdept. rdlc "; this. reportviewer1.localreport. datasources. add (New reportdatasource ("ds_dept", getdeptdata () ;}} datatable getdeptdata () {datatable dt = new datatable (); DT. columns. add ("deptno", typeof (string); DT. columns. add ("deptname", typeof (string); DT. rows. add ("01", "Office"); DT. rows. add ("02", "Technology Department"); DT. rows. add ("03", "Sales Department"); DT. rows. add ("04", "Customer Service Department"); Return DT ;}}}
Running effect:
OK. The following is the real start:
In many cases (such as team development), the data source able of a report is usually written by others, and some do not even allow modifications. Report developers can only passively receive data, however, the report does not necessarily need to display all the data. For example, if we only need to display the data of "02 Technology Department", what should we do?
In this case, the report parameters come in handy:
4. Add Report Parameters
On the report data panel, select parameters and right-click --> Add Parameter
Name the parameter deptno and make some settings, such
5. Add filters conditions for the table of the report
The parameters added in the previous step need to be associated with the table on the report, otherwise they will not work. Fortunately, you can set a filters expression for each table to filter data. For details, see:
6. dynamically input parameters in CS code
Modify the default. aspx. CS code and dynamically add parameters during runtime.
Protected void page_load (Object sender, eventargs e) {If (! Ispostback) {This. reportviewer1.localreport. reportpath = "rptdept. rdlc "; this. reportviewer1.localreport. datasources. add (New reportdatasource ("ds_dept", getdeptdata (); // dynamically input the parameter This. reportviewer1.localreport. setparameters (New reportparameter ("deptno", "02 "));}}
Final running result:
In many reports, data is often sourced from more than one able. Next we simulate a simple master-slave report. The main report is the preceding rptdept (displaying department information ), A subreport (also known as a report) shows a list of employees under a department (named rptemp. rdlc)
VII. Create employee report rptemp. rdlc
The layout is as follows:
Similarly, we also add a parameter deptno for the subreport, and set the filters condition for the table of the subreport (the value of the condition is the same as that in the main report in this example, both deptno = @ deptno)
8. Insert the subreport rptemp. rdlc into rptdept. rdlc.
The subreport control allows you to insert another report in one report, for example:
Right-click the subreport to bring up the attributes of the subreport.
Set which subreport to load
Add a subreport Parameter
Note: A deptno parameter with the same name as the main report is added here, and its value is also set as the parameter @ deptno for the main report rptdept.
9. Modify the default. aspx. CS code
Using system; using system. data; using Microsoft. reporting. webforms; namespace reportsample {public partial class default: system. web. UI. page {protected void page_load (Object sender, eventargs e) {If (! Ispostback) {// define the subreport processing method this. reportviewer1.localreport. subreportprocessing + = new subreportprocessingeventhandler (localreport_subreportprocessing); this. reportviewer1.localreport. reportpath = "rptdept. rdlc "; this. reportviewer1.localreport. datasources. add (New reportdatasource ("ds_dept", getdeptdata (); // dynamically input the parameter This. reportviewer1.localreport. setparameters (New reportparameter ("deptno", "02") ;}} void localreport_subreportprocessing (Object sender, subreportprocessingeventargs e) {e. datasources. add (New reportdatasource ("ds_emp", getempdata ();} datatable getdeptdata () {datatable dt = new datatable (); DT. columns. add ("deptno", typeof (string); DT. columns. add ("deptname", typeof (string); DT. rows. add ("01", "Office"); DT. rows. add ("02", "Technology Department"); DT. rows. add ("03", "Sales Department"); DT. rows. add ("04", "Customer Service"); Return DT;} datatable getempdata () {datatable dt = new datatable (); DT. columns. add ("empno", typeof (string); DT. columns. add ("empname", typeof (string); DT. columns. add ("deptno", typeof (string); DT. rows. add ("001", "Yang Guo", "02"); DT. rows. add ("002", "Ling Hu Chong", "02"); DT. rows. add ("003", "Huang Rong", "01"); DT. rows. add ("004", "", "03"); DT. rows. add ("005", "Zhao Min", "04"); Return DT ;}}}
Final running effect:
Think about what happened?
The same parameters and filtering conditions are set for the primary report rptdept and the sub-Report rptemp. After the code passes the deptno parameter to the main report rptdept, the main report rptdept passes the parameter value to the sub-Report rptemp, the final two reports achieve data filtering.