Walkthrough: Using a Business Object Data Source with the ReportViewer Windows Forms Control in Local Processing Mode

This walkthrough shows how to use an object data source using Business objects in a report in a Microsoft Visual Studio 2008 Windows application. For more information about Business objects and object data sources, see Binding to Business Objects.

Perform the following steps to add a report to a Visual Studio Windows application project. For this example, you will be creating the application in Microsoft Visual C#.

Create a new Windows application project

  1. On the File menu, point to New, and select Project.

  2. In the New Project dialog, under Visual C#, choose the Windows Application template.

  3. Type BusinessObject for the project name and click OK.

Create business objects to use as a data source

  1. From the Project menu, select Add New Item.

  2. In the Add New Item dialog, choose Class, type BusinessObjects.cs for the file name, and click Add. The new file is added to the project and automatically opened in Visual Studio.

  3. Replace the default code for BusinessObjects.cs with the following code:

    using System;
    using System.Collections.Generic;
    // Define the Business Object "Product" with two public properties
    //    of simple datatypes.
    public class Product {
        private string m_name;
        private int m_price;
        public Product(string name, int price) {
            m_name = name;
            m_price = price;
        public string Name {
            get {
                return m_name;
        public int Price {
            get {
                return m_price;
    // Define Business Object "Merchant" that provides a 
    //    GetProducts method that returns a collection of 
    //    Product objects.
    public class Merchant {
        private List<Product> m_products;
        public Merchant() {
            m_products = new List<Product>();
            m_products.Add(new Product("Pen", 25));
            m_products.Add(new Product("Pencil", 30));
            m_products.Add(new Product("Notebook", 15));
        public List<Product> GetProducts() {
            return m_products;
  4. From the Project menu, select Build Solution. This creates an assembly for the object, which you will later use as a data source for the report.

Add a report to the project

  1. Make sure the project or a project item is selected in Solution Explorer.

  2. From the Project menu, select Add New Item.

  3. In the Add New Item dialog, select Report. Type a name for the report and click Add. The report is added to the project and automatically opened in Report Designer. By default, the report name is Report1.rdlc.

Use the Data Source Configuration Wizard to create a data sources

  1. Make sure the project or a project item is selected in Solution Explorer.

  2. From the Data menu, select Add New Data Source. This launches the Data Source Configuration Wizard.

  3. On the Choose a Data Source Type page, select Object and click Next.

  4. The Data Source Configuration Wizard displays a list of existing classes in the project. Expand the class hierarchy under BusinessObjects until you see Product in the list. Select Product and click Next,and then click Finish.

    When the Wizard closes, the new data source object appears in the Data Sources window.

Design the report

  1. With the report open in Design mode, open the Toolbox. From the Toolbox, drag a Table control onto the report. The table control appears in the report's design view window. Note that the dotted background represents the dimensions of the report body and the table control can be resized or repositioned.

  2. From the Data Sources window, drag the Name field from the Product data source onto the first column of Detail row of the table. The Detail row is the middle row. Notice that the Header row automatically fills in for you when you specify the Detail row.


    If the Data Sources window is not visible, select Show Data Sources from the Data menu.

  3. Drag the Price field onto the Detail row of the second column, so that it is next to the Name field. Optionally, select the header row of the table by clicking on the left table header icon and apply the Bold font style.

  4. Delete the unused third column. Click the third column, then click on the header bar, and press the Delete key.

  5. To add a title to the report, open the Toolbox and drag a Textbox control onto the report. Position the Textbox above the table. Type Products for the report name.

  6. (Optional) Apply font size and font style to the text to make the title stand out.

Add a ReportViewer control to the report

  1. Select the automatically-generated Windows application form in Design view. By default, the form name is Form1.cs.

  2. Open the Toolbox. In the Toolbox, expand the Data node and drag the ReportViewer icon onto the form. Expand the form and reposition the ReportViewer as needed.

  3. Select the ReportViewer control, and open the smart tags panel by clicking the triangle on the top right corner. Click the Choose Report drop-down list and select the report you just designed. By default, the name is Report1.rdlc. Notice that a BindingSource is automatically created corresponding to each object data source used in the report.

Supply data source instances to the BindingSource object

  1. Right-click on the Visual Studio form and choose View Code from the shortcut menu.

  2. In Form1.cs, add the following code to the top of the class. You can use the first line after the public partial class Form1 : Form { statement, but before the constructor.

    // Instantiate the Merchant class.
    private Merchant m_merchant = new Merchant();
  3. In the Form1_Load() method, add the following code as the first line, before the RefreshReport call:

    // Bind the Product collection to the DataSource.
    this.ProductBindingSource.DataSource = m_merchant.GetProducts();

Run the application

  • Press F5 to run the application and view the report.

See Also




Using the ReportViewer Tasks Smart Tags Panel

Other Resources

Samples and Walkthroughs