Creating Data Sources for a Report
A client report definition (.rdlc) file includes dataset elements that define the data source structure used by the report. You must add one or more datasets to the report before you can use the data in your report definition. After creating a dataset, you can drag a specific field to a data region or text box in the report.
Client report definition (.rdlc) files are processed by the ReportViewer control as local reports. Unlike server reports, local reports require data to be processed before the reports can be processed by the ReportViewer control. Local reports can use data from any source as long as it can be supplied as a DataTable or an IEnumerable collection of business objects. The data table or business object returns a list of fields that can be used by the report. Each field contains a pointer to a database field and a name property, a field in an object data source, or a column in a DataTable. You can drag fields from the Report Data window onto the design surface of your report.
The following approaches are recommended for setting up a DataSet or an IEnumerable enumeration of business objects as a report data source. After a data source has been configured, you can bind the DataSet or business objects to your report.
To create a DataTable, use the Add New Item command on the Project menu and select the DataSet object. Drag a TableAdapter from the Toolbox to the DataSet Editor to configure the DataTable with the TableAdapter Wizard. The TableAdapter Wizard provides a query builder and data preview feature so that you can confirm query results right away.
A business object is any application object that exposes public properties of simple data types. For example, an array of Book Title objects created by your application is a business object that can be used as a data source for your report.
To be accessible as a data source, a class must expose a method or property that returns an IEnumerable. You can add a class or a reference to the assembly for a class to your project.
Steps for using business objects vary between Windows Forms and ASP.NET projects. For instructions on using business objects in Windows Forms, see Walkthrough: Using a Business Object Data Source with the ReportViewer Windows Forms Control in Local Processing Mode. For instructions on using business objects in ASP.NET, see Walkthrough: Using a Business Object Data Source with the ReportViewer Web Server Control in Local Processing Mode.
Report definition (.rdlc) files are bound to DataSet objects or business objects that contain the data you need. When you create a report using the Report Wizard, the Report Wizard takes you through the steps of creating a DataSet object with the data you need and binding it to the report. You can also manually bind a DataSet or business object to a report in the Report Data window. This window displays all datasets that are bound to the report, grouped by the DataSet objects.
In the Report Data window of Report Designer, click New, then select Dataset.
In the Data source list box, select the DataSet object you created. To create a new DataSet, click New and walk through the Data Source Configuration Wizard provided by Visual Studio. If you created a business object, select global to access the business object.
In the Available datasets list box, select the data you want to bind to. This list box displays the available DataTable or business objects.
In the Name box, specify a name for this dataset, then click OK.
If you later modify the DataSet schemas after the report is defined, you must update the data bindings in the report and control. For more information, see Updating and Rebinding Data Source References.
You can have any number of datasets in your report. To view the list of datasets actually used by a report, select Report Data on the View menu. To view the report and its data in your application, you must build or deploy the application to verify that the report contains the data you expect. For more information about configuring the control and defining the report layout, see Configuring ReportViewer for Local Processing and Creating Client Report Definition (.rdlc) Files.
To get started immediately, use the following walkthroughs to learn how to add each type of data source to a project:
Data source controls provide support for abstracting the data access layer so that controls on a Web page bind to a data source, which in turn binds to the actual data. This allows you to efficiently change the data source without having to rebind every control to every piece of data.
When you are designing a Web page, the Data section in the Toolbox lists several data source controls like SqlDataSource and XmlDataSource. Some data source controls provide support for Page memory in Web pages and others use shared data. Reports do not use Page memory, so the best choice for a ReportViewer Web server control is to use the ObjectDataSource control. This control provides a bridge between in-memory cached objects (DataTable or business object collections) and the report. When you add a ReportViewer control to a Web page, a ObjectDataSource control is automatically added and configured.