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 Windows Forms 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 Windows Forms application project. For this example, you will be creating the application in Microsoft Visual C#.

Create a new Windows Forms application project

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

  2. In the New Project dialog box, in the Installed Templates pane, choose Visual C#, and then choose the Windows Forms Application template. The C# node may be under Other Languages, depending on your startup settings in Visual Studio.

  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 box, 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 using the Report Wizard

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

  2. In the Add New Item dialog, select Report Wizard. Type a name for the report and click Add.

    This launches the Report Wizard with the Data Source Configuration Wizard.

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

  4. In the Select the Data Objects page, expand the class hierarchy under BusinessObject until you see Product in the list. Select Product and click Finish.

    You now return to the Report Wizard. Notice that the new data source object is added to your project in Solution Explorer.

  5. In the Dataset Properties page, in the Data source box, verify that global is selected.

  6. In the Available datasets box, verify that Product is selected.

  7. Click Next.

  8. In the Arrange Fields page do the following:

    1. Drag Name from available fields to the Row groups box.

    2. Drag Price from available fields to the Values box.

  9. Click Next twice, then click Finish.

    This creates the .rdlc file and opens it in Report Designer. The tablix you created is now displayed in the design surface.

  10. Save the .rdlc file.

Add a ReportViewer control to the report

  1. In the Solution Explorer, open the Windows form in Design view. By default, the form name is Form1.cs.

  2. From the Toolbox, in the Reporting group, drag the ReportViewer icon onto the form.

  3. In the ReportViewer control open the smart tags panel by clicking the smart-tag glyph on the top right corner.

  4. In the Choose Report list, select the report you just designed. By default, the name is Report1.rdlc. Notice that a BindingSource object called ProductBindingSource is automatically created corresponding to each object data source used in the report.

  5. In the open smart tags panel, choose Dock in parent container.

Supply data source instances to the BindingSource object

  1. In the Solution Explorer, right-click Form1.cs and select View Code.

  2. In Form1.cs, inside the partial class definition, add the following code as the first line, 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

Reference

ReportViewer.Drillthrough

LocalReport.SubreportProcessing

ReportViewer.Drillthrough

LocalReport.SubreportProcessing

Concepts

Using the ReportViewer Tasks Smart Tags Panel

Other Resources

Samples and Walkthroughs