Reporting in .NET
Most business applications require some type of reporting. Why bother with all of the CRUD (create, review, update, delete) if there is no reporting on all that data?
With .NET, there are several reporting options and features. This set of posts focuses on two features:
- Report file: An installed template in Visual Studio for building the layout for a report.
- ReportViewer: A control in the toolbox that can be added to a form for displaying the report to the user.
The example below provides an overview of building the "world’s simplest report". In building the "simplest" report, there are still a very large number of steps. So this post provides an overview and other referenced posts provide detail.
To Create a Report:
1) Add a report file to your project.
Select Project | Add New Item, click on Reporting under Installed Templates on the left and then select Report (or Report Wizard if you want to use the Wizard) in the middle.
NOTE: Though using the Report Wizard could arguably be the best approach when building the "world’s simplest report", when I first starting building reports with these tools I found the Wizard to be confusing (DataSource, DataSet, Row group, Column group, etc) . So for clarity, these posts walk through the manual steps.
Clicking Add adds the .rdlc file to your project. In this example, the report file is named Customers.rdlc.
2) Build the report layout using the tools in the toolbox. (View | Toolbox)
See this link for more information on building the layout for a report.
3) Tie the report layout to your data using the tools in the Report Data window. (View | Report Data)
- To tie the report directly to the database, see this post.
- To tie the report to business objects, see this post.
When you build reports within Visual Studio, there is no mechanism for previewing the report with its data. You have to build your own user interface for displaying the report.
To Display a Report:
1) Add a form to your project.
NOTE: You can add a Windows Form or a Web form. This example uses a Windows Form.
2) Add the Report Viewer control from the toolbox to the form. (View | Toolbox)
You may want to dock the Report Viewer control within the parent container so the report fills the entire form.
3) Tie your report to the Report Viewer control.
The easiest way to tie your report to the Report Viewer is to use the Report Viewer’s smart tag:
Select the name of the report from the Choose Report drop down list.
4) Tie your dataset to the Report Viewer control.
The easiest way to tie your report dataset to the Report Viewer is to use the Report Viewer’s smart tag and select Choose Data Sources. Then select the data source defined in Step 3 of "To Create a Report".
5) Set the new form as your startup form if you want to run the report directly when launching your application for testing purposes.
See this post for instructions on setting a form as a startup form.
Otherwise, add navigation in your application that allows the user to navigate to the report either through a link, menu option, toolbar icon, or other mechanism.
The resulting report looks something like this:
Use this technique any time you need to add reporting to your application.