Setting Up the ReportViewer Control
To have VS generate most of the UI code, you can leverage the TableAdapter and VS’s ability to set up the UI elements to capture the input parameter, Style-Wanted, for the initial query. StyleWanted should be sufficient because you won’t need any UI elements to browse through the rowset or make changes. The following steps take you through setting up the ReportViewer Control:
1. Open the Form1 design-mode window.
2. Drag the Product data set from the Data Sources window to the form design surface. Doing so adds the ProductTableAdapter to the form’s generated code. It also adds a label and textbox to capture the parameter, and a Fill button to launch the Fill method defined for the TableAdapter. In addition, it supplies a DataGridView control, and Table-AdapterManager, BindingSource, and BindingNavigator classes that you don’t need.
3. Delete these unneeded classes; they’re used to provide update and scrolling mechanisms that you don’t need. All you want is the tool strip to manage the parameter and expose the Fill button. The result should look like Figure 5.
4. Now drag the ReportViewer control to the form. Immediately, the control prompts for the name of the RDLC report file or the path to an SSRS server-hosted report. It will look like Figure 6, page 30. Note that the control has been renamed Microsoft ReportViewer in VS 2008, and the control has been moved to the Reporting section of the toolbox.
When you add the ReportViewer control to the form or rebind the Data Source, VS drops a line or two of code into the Form_Load event handler because it doesn’t know where else to put it. However, you shouldn’t leave the code there. Instead, move the Fill method and RefreshReport method calls to the ToolStrip Button_Click event handler so they’re executed after you’ve captured the desired Style parameter from the user. Incidentally, the generated code won’t work because it ignores the fact that the Fill method requires an input parameter. See Web Listing 3 for the code.
When you enter a style value (such as “M”) in the ToolStrip text box and click the ToolStrip Fill button, this code executes the parameterized Fill method, binds the rowset to the Report DataSet, and shows the report hosted in the ReportViewer control.
Configuring the Clickthrough Report
At this point, the initial Products By Profit report should work. Now you need to have ReportViewer call the routine that invokes the StockReport FillBy-ProductID method and bind to the generated Data-Table. The following steps show you how to configure the clickthrough report:
1. First, make sure your form’s Class includes an Imports statement to help the compiler resolve the Microsoft.Reporting.WinForms namespace. The Imports statement permits VS to see the ReportViewer DrillthroughEventArgs: Imports Microsoft.Reporting.WinForms
2. Next, set up an input parameter for the Stock-Report using the VS Report Designer. As a reminder, click the StockReport in the designer and select Report, Report Parameters.
3. Add a new parameter. Figure 7, page 30, shows that the ProductIDWanted parameter is defined.
4. Mark the parameter as hidden because it’s provided only in code.
Coding the Drillthrough Event Handler
The next step is to respond to the Drillthrough event that’s fired when a user clicks the report cell (text box) that’s programmed to navigate to the Stock Report. This routine (shown in Web Listing 4) ensures that it’s the Stock Report that’s been requested.
Now you need to execute the parameter-driven Fill method and point the StockReport Data Source to the DataTable that’s returned. The code in Web Listing 5 handles this task.
The comments in this routine make it fairly self-explanatory. The base ProductProfitByStyle report should now run (after a style parameter is provided). When the user clicks the Product Number text box cell, the ClickThrough expression is programmed to run the stock query and display a report with the new data. To get back to the first report, the user needs to click the Back button (green arrow) on the ReportViewer.
Infinite Flexibility with ReportViewer
Congratulations. You now know how to set up a clickthrough report without using SSRS. In SSRS, there’s no need for extra code to generate the data sources because the code is written at design time and re-executed at runtime. However, using the VS ReportViewer control without SSRS gives you infinite flexibility because you can call whatever methods you desire to return the rowset consumed by the clickthrough report.
End of Article
Prev. page
1
[2]
next page -->