This article uses the customers and orders tables in the msserver's built-in northwind library file as examples to create two report files. After querying the customers table data, click customerid Data in this table, the report is transferred to the next report, and the related orders data is displayed, that is, the so-called drilling report in.
1. Open a project, create a new from, and put a button and a reportviewer control.
2. Create a new data source in the project and connect to the northwind database file. The customers and orders tables are displayed.
3. Create a report file and display data in a table. Put the customerid, companyName, and address fields in the MERs table, form a simple report with a header and data, and save the report named mermerreport.
4. Create a report file, which is the same as customerreport. The report is also displayed in a table. Put the orderid, customerid, shipname, and orderdate fields in orders into the table, save the report as orderreport
5. The orderreport data source determines the data of the Data Source Based on the customerreport data in the main report. view the dataset name of the table. I will display it as northwinddataset_orders. You can also create one by yourself, for details, see my rdlc Report (2).
6. Open the customerreport report and set customerid as an index field that can be clicked to transfer it to the next report. Select the customerid field, right-click the field, select the "navigation" tab in the "text box properties" window, and select "Jump to report" in the "HYPERLINK" below. select "orderreport" in the report name, and then press the "parameter... "button, enter a parameter name, such as customerid, and select = Fields! Customerid. value. To distinguish it from other data, you can change the color or underline of this column based on your habits.
7. Set a report parameter in orderreport, which is the same as the name in curtomerreprot to receive parameters passed in the parent table.
8. Create two methods for getting data. One method is to get a dataset from MERs, And the other method is to get a dataset from orders with parameters. These two methods can be implemented by writing a class library or adding them to the vs dataset. For ease of demonstration, I directly used getdata () of MERs and wrote a method of getdatabycustomerid (@ CID) of orders.
9. Write the following code in the from button: Code : Private Void Button2_click ( Object Sender, eventargs E)
{
Northwinddataset. customersdatatable dt1 = New Northwinddatasettableadapters. customerstableadapter (). getdata ();
This . Reportviewer1.localreport. reportembeddedresource = " Testreport. customerreport. rdlc " ;
This . Reportviewer1.localreport. CES. Clear ();
This . Reportviewer1.localreport. CES. Add ( New Reportdatasource ( " Northwinddataset_mers MERs " , Dt1 ));
This . Reportviewer1.refreshreport ();
}
10. Use the drillthrough event of the report. This event occurs when a drill item is selected. The code for the next report is as follows: Private Void Reportviewer1_drillthrough ( Object Sender, drillthrougheventargs E)
{
Localreport lp = (Localreport) E. Report;
String Customerid = LP. getparameters ()[ " Customerid " ]. Values [ 0 ]. Trim ();
LP. datasources. Clear ();
LP. datasources. Add ( New Reportdatasource ( " Northwinddataset_orders " ,
New Northwinddatasettableadapters. orderstableadapter (). getdatabycustomerid (customerid )));
}
Shipping result:
Run the first report:
Run the second report after drilling: