Object data sources

You can design reports based on business objects defined in your application.

An object data source is simply a collection of arbitrary objects. A collection is an array, List or any other class that implements the IEnumerable interface. Objects in the collection must be of the same type, but need not implement any particular interface or inherit from any particular class.

Instances of a class with public properties are functionally equivalent to rows in a database table, and the public properties are functionally equivalent to columns of a database table.

Example

Here's an example of a class that can be used as an object data source:

Nested objects

Only the first level of public properties can be dragged and dropped to a report. If the type of a property is a custom class with its own properties then you can manually enter an expression to access the values of second level properties. For example if the Employee class has a HomeAddress property of type Address then you can enter the expression =Fields!HomeAddress.Value.Zip to access the value of Zip property. Here's an example.

If the value of a property is a collection then you can use a subreport to display the collection. See this example.

Using object data sources in Windows applications

To add an object data source in a WinForms application, from the menu choose Data > Add New Data Source. In the Data Source Configuration Wizard choose Object and click Next. Select the class you wish to use as a data source and click Finish. The public properties of the class now appear in the Data Sources window, where they can be dragged and dropped into the report.

After designing the report, drag and drop ReportViewer control into your form. In the smart tags panel of the ReportViewer control select the report you just designed. A BindingSource object is automatically generated for you. The DataSource member of this BindingSource object must be set to a collection of your objects.

Using object data sources in Web sites

To add an object data source to a web site choose 'Add New Item' from Website menu and select Class. You are prompted whether you want to place the class in App_Code folder. Answer Yes. Implement the class.

There is no separate step to make the class appear in the Website Data Sources window. All classes that qualify as object data sources automatically appear in the Website Data Sources window. In order for a class to qualify as an object data source, there should be a "Select method" that returns a collection of objects of that class.

The Select method

In the case of Web sites, in addition to a class that provides the fields you should add a class that has a select method. The "Select method" is any method that returns a strongly typed collection of objects. The select method may have parameters. In the example below the select method is GetProducts().

To find classes that qualify as object data sources, Report Designer first looks for select methods, i.e., methods that return strongly-typed collections of objects that have public properties. The types of objects returned by these select methods are then added to the Website Data Sources window. If a particular type is returned by more than one select method then that type appears more than once in the Website Data Sources window. To distinguish one instance of a type from another, the class and signature of the select method are displayed next to the type.

After designing the report drag and drop the ReportViewer control into your form and select the report using the Smart Tags panel. ReportViewer automatically creates object data source instances corresponding to each data source used in the report. Since ReportViewer already knows about the select method there is no need to write any additional code to supply the collection of objects.

If a select method has parameters then click the 'Configure Data Sources' command from the Smart Tags panel of the object data source in order to choose sources for the parameters' values.