Walkthrough: Creating a ReportViewer Report
This walkthrough shows how to create a simple table report in a Visual Studio Windows Forms application project based on the AdventureWorks2008 sample database. You will add a report to your project using the Report Wizard and add a ReportViewer Windows Forms control to a Windows Form so the report can be viewed by users of the application.
For a demonstration of how to create a drillthrough report using the reportviewer control, see Create a Drillthrough (RDLC) Report with Parameters using ReportViewer (SSRS Tutorial).
For more information on designing reports with the Visual Studio Report Designer, see Report Designer (Visual Studio).
Prerequisites
To use this walkthrough, you must have access to the AdventureWorks2008 sample database. The query used in this walkthrough will not work with an earlier version of AdventureWorks. For more information about how to get the AdventureWorks2008 sample database, see Walkthrough: Installing the AdventureWorks Database.
This walkthrough assumes that you are familiar with Transaction-SQL queries and ADO.NET DataSet and DataTable objects.
To create a new Windows Forms application project
Open Visual Studio. On the File menu, point to New, and then select Project.
In the Installed Templates pane, expand Other Languages and select Visual Basic.
Choose Windows Forms Application.
In the Name box, type SimpleReport.
In the Location box, enter the directory in which you want to save your project, or click Browse to navigate to it.
Click OK.
The Windows Forms Designer opens, showing Form1 of the project you created.
Click on the form. From the View menu, choose Properties Window. Expand the Size property to show Width and Height. Set Width to 500 pixels.
To define a data set and a data table
In the Project menu, select Add New Item.
In the Add New Item dialog, click DataSet. Type a name for the dataset and click Add. The default name is DataSet1.xsd.
This adds a new XSD file to the project and opens the Dataset Designer.
From the Toolbox in the Dataset Designer, drag a TableAdapter control onto the design surface.
This starts the TableAdapter Configuration Wizard.
On the Choose Your Data Connection page, click New Connection.
If this is the first time you create a data source in Visual Studio, you will see the Choose Data Source page. In the Data Source box, select Microsoft SQL Server.
In the Add Connection dialog box, perform the following steps:
In the Server name box, enter the server where the AdventureWorks2008 database is located.
The default SQL Server Express instance is (local)\sqlexpress.
In the Log on to the server section, select the option that provides you access to the data. Use Windows Authentication is the default.
From the Select or enter a database name drop-down list, click AdventureWorks2008.
Click OK to continue to return to the wizard, then click Next.
If you specified Use SQL Server Authentication in the previous step, select the option whether to include the sensitive data in the string or set the information in your application code.
On the Save the Connection String to the Application Configuration File page, type in the name for the connection string or accept the default AdventureWorks2008ConnectionString. Click Next.
On the Choose a Command Type page, select Use SQL Statements, and click Next.
On the Enter a SQL Statement page, enter the following Transact-SQL query to retrieve sales data from the AdventureWorks2008 database, and then click Finish:
--SET DATEFORMAT mdy SELECT PC.Name AS Category, PS.Name AS Subcategory, DATEPART(yy, SOH.OrderDate) AS Year, 'Q' + DATENAME(qq, SOH.OrderDate) AS Qtr, SUM(DET.UnitPrice * DET.OrderQty) AS Sales FROM Production.ProductSubcategory PS INNER JOIN Sales.SalesOrderHeader SOH INNER JOIN Sales.SalesOrderDetail DET ON SOH.SalesOrderID = DET.SalesOrderID INNER JOIN Production.Product P ON DET.ProductID = P.ProductID ON PS.ProductSubcategoryID = P.ProductSubcategoryID INNER JOIN Production.ProductCategory PC ON PS.ProductCategoryID = PC.ProductCategoryID WHERE (SOH.OrderDate BETWEEN ('20020101') AND ('20031231')) GROUP BY DATEPART(yy, SOH.OrderDate), PC.Name, PS.Name, 'Q' + DATENAME(qq, SOH.OrderDate), PS.ProductSubcategoryID
You can also click on the Query Builder button and use Query Builder to create a query and validate it using the Execute Query button.
Click Finish.
The Dataset Designer now shows the DataTable definition for DataTable1 with fields named from the columns and column aliases of the query (Category, Subcategory, Year, Qtr, and Sales). You will use these fields when binding data to your report.
Note
If you need to change the fields in your data table, right-click on the DataTable1 header or the DataTable1TableAdapter header on the Dataset Designer page. Choose Configure, which restarts the TableAdapter Configuration Wizard.
Save the DataSet1 file.
To add a new report definition file using the Report Wizard
From the Project menu, select Add New Item.
In the Add New Item dialog box, choose Report Wizard.
In Name, type Sales Orders.rdlc and then click Add.
A graphical design surface opens behind the dialog box.
In the Dataset Properties page, in the Data source drop-down list, select the DataSet you created.
The Available datasets box is automatically updated with the DataTable you created.
Click Next.
In the Arrange Fields page do the following:
Drag Category from available fields to the Row groups box.
Drag Subcategory from available fields to the Row groups box, below Category.
Drag Year from available fields to the Column groups box.
Drag Qtr from available fields to the Column groups box, below Year.
Drag Sales from available fields to the Values box.
Click Next twice, then click Finish.
This creates the .rdlc file and opens it in Report Designer. The tablix you designed is now displayed in the design surface.
To add a title to the report
Click the tablix on the designer surface so that column and row handles appear above and next to the table.
Note
Handles are gray boxes that appear above and next to the tablix. You use handles to perform various actions on columns groups, row groups, and the tablix itself. The handles that run across the top of the tablix are column handles. The handles that run down the side of the tablix are row handles. The handle where the column and row handles meet is the corner handle.
Click the corner handle to select the entire tablix to show the cross arrow.
Move the tablix down on the design surface by dragging the cross arrow.
From the Toolbox, drag a Text Box item to the area above the tablix in the design surface, and then position it by dragging the cross arrow.
With the textbox selected, type Company Sales. If necessary, drag the textbox border to expand it.
With the textbox selected, click the Bold button in the Report Formatting toolbar, and then select 16pt in the Fond Size box.
With the textbox selected, click the Foreground Color button in the Report Formatting toolbar.
In the Choose Color dialog box, select the Midnight Blue color and click OK.
To add the ReportViewer control to your form
Click Form1.vb in Solution Explorer.
From the View menu, choose Designer.
From the Reporting section of the Toolbox, drag the ReportViewer control to the form.
Open the smart tags panel of the ReportViewer1 control by clicking the smart-tag glyph on the top right corner. Click the Choose Report drop-down list and select SimpleReport.Sales Orders.rdlc.
From the smart tags panel, click Dock in parent container.
Throughout the rest of this walkthrough, you can build your application and view the report in the form at any time. If you want to see how the incremental changes of your report design affects the end report, build and view the report as the last step in each of the following procedures.
(Optional) Press F5 to build your application and view the report in the form.
To format a currency field
In the Report Designer window for the Sales Orders.rdlc file, right-click the upper-left cell with the [Sum(Sales)] value and then click Text Box Properties.
Select the Number tab.
In the Category list, select Currency.
In the Symbol list, select English (United Kingdom).
Click OK to close the dialog box.
Perform the same steps to all other cells with the [Sum(Sales)] value.
(Optional) Press F5 to build your application and view the report. Observe the change of the number format. Note that no currency conversion is made, but only the number format is changed.
To format tablix layout
In the Report Designer window for the Sales Orders.rdlc file, drag the cursor to select the two empty cells in the top left corner of the tablix.
Right-click the selected cells and select Merge Cells.
Right-click the merged cell and select Text Box Properties.
Click the Border tab. In the Preview area, click the border toggle buttons to remove the top and left borders.
Click OK.
Click the tablix to show its handles. For the column with the header Subcategory, expand the column width by dragging the right side of the column handle. This way you can prevent the header text from wrapping to the next line.
Press F5 to build your application and view the report.
See Also
Reference
LocalReport.SubreportProcessing
LocalReport.SubreportProcessing
Concepts
Using the ReportViewer Tasks Smart Tags Panel
Other Resources
Create a Drillthrough (RDLC) Report with Parameters using ReportViewer (SSRS Tutorial)
Designing and Implementing Reports Using Report Designer (Reporting Services)