A First Look at the VS2008 ReportViewer
What's different in this case is that, when the Wizard is done, the finished form already has been customized to include the data-bound ReportViewer control as well as the code in Form_Load to execute the SELECT query and launch the report as shown in Listing 1.
Public Class Form1 Private Sub Form1_Load(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles MyBase.Load 'TODO: This line of code loads data into the 'BiblioDataSet.Customers' table. You can move, 'or remove it, as needed. Me.CustomersTableAdapter.Fill(Me.BiblioDataSet.Customers) Me.ReportViewer1.RefreshReport() End Sub End Class
Listing 1: Code automatically added to the Form_Load event handler.
As I discuss in my lectures and in the long chapter in my book on the Report Viewer control, this is not an ideal place for this code, but I understand that the Wizard does not have a lot of other choices. One of the first chores you should take on is to create a button on the form to execute these two lines of code—one that runs the query, the next that launches the report processor.
Running the Report
Okay, at this point you actually can run the project and see the new report. It might take quite some time to execute because it has to return all of the rows of the target table to the client and construct the report in memory. Think about the steps you need to take to make this a more useful report:
- Focus the rowset used to generate the report. This entails creating a DataTable (strongly typed or untyped) that accepts one or more parameters (or at least a hard-coded WHERE clause) to focus the rowset on the desired data. Ideally, the user should have some control over the data exposed by the report.
- This also means you'll have to code your own UI code to capture and validate the parameters and pass these query parameters to the LocalReport class before rendering the report.
- You'll also want to reformat the columns so that the column widths, numbers, and dates are properly formatted and the groups and page breaks occur where they should. As it is, the wizard does not do much of this housekeeping for you.
Working with Stored Procedures
The problem is that, if you specify a parameter-driven stored procedure to return the rowset, the Wizard does not add the call to the TableAdapter Fill method to the Form_Load event handler. To wire this up, you'll need quite a few more steps and a mechanism to capture the query parameter values at runtime.
It's a shame that the report wizard does not take this fairly basic approach because without it, the Report Wizard makes pretty good demoware, but not much else.
About the Author
William (Bill) Vaughn is an industry-recognized author, mentor, and subject-matter expert on Visual Studio, SQL Server, Reporting Services, and data access interfaces. He's worked in the computer industry for over thirty-five years—working with mainframe, minicomputer, and personal computer systems as a developer, manager, architect, trainer, marketer, support specialist, writer, and publisher. In 2000, after 14 years at Microsoft, Bill stepped away to work on his books, mentoring, and independent training seminars. He's written seven editions of the Hitchhiker's Guide to Visual Basic and SQL Server, and three editions of ADO.NET and ADO Examples and Best Practices for Visual Basic (and C#) Programmers. He and Peter Blackburn also wrote the critically acclaimed Hitchhiker's Guide to SQL Server 2000 Reporting Services.
Bill is a top-rated speaker and frequents conferences all over the world, including TechEd, Visual Studio/SQL Connections, DevTeach, and many others. He's also written a wealth of articles for magazines such as MSDN, SQL Server Magazine, Visual Basic Programmer's Journal, .NET Magazine, and many others as well as a regular editorial for Processor magazine. Bill spends considerable time answering questions on the public newsgroups and speaking at INETA user group meetings all over the country and at other speaking venues all over the world. He's available for consulting, mentoring, or custom training. See www.betav.com or www.betav.com/blog/billva for his current schedule and course catalog.
Page 2 of 2