This documentation is archived and is not being maintained.

How to: Add an Object as a Project Data Source

You can create data sources that are based on your objects in a Visual Studio application. After you define an entity type as a data source in a project, you can create forms that display entity data by dragging items from the Data Sources window onto forms. These items become controls on the form that are bound to the data source. For more information, see Binding Objects to Controls

In this topic, you will create a data source for the SalesOrderHeader type in the Adventure Works Sales Model. You will then use this data source to create a Windows form with controls that are bound to entity data. To complete these procedures, you must have already added the AdventureWorks Sales model to your project and configured your project to use the Entity Framework. To do this, complete the procedures in How to: Use the Entity Data Model Wizard.

To create a data source based on the SalesOrderHeader types

  1. If you have recently added the Entity Data Model item, build the project.

  2. On the Data menu, click Add New Data Source.

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

  4. On the Select an Object You Wish to Bind to page, expand the project node, expand the project's namespace node, and select the SalesOrderHeader type in the tree view.

  5. Click Finish.

    The SalesOrderHeader data source is added to the Data Sources window.

To add data source bound controls to a Windows form

  1. In the Data Sources window, expand the SalesOrderHeader node.

  2. Drag one or more properties from the SalesOrderHeader node to the form.

    This creates the salesOrderHeaderBindingSource and salesOrderHeaderBindingNavigator controls on the form. A data-bound control is also created on the form for each property, accompanied by an appropriately titled label control.

  3. Drag the SalesOrderDetail navigation property to the form.

  4. This creates the salesOrderDetailBindingSource control with the DataSource property of the control set to the salesOrderHeaderBindingSource and the DataMember property set to SalesOrderDetail. It also creates the salesOrderDetailDataGridView data-bound control on the form, accompanied by an appropriately titled label control.

To bind the data source to the result of an object query

  1. Open the code page for the form and add the following using statements (Imports in Visual Basic):

    using System.Data.Objects;
  2. In the partial class that defines the form, add the following code that creates an ObjectContext instance and defines the customerID constant.

    private AdventureWorksEntities context;
    private const int customerId = 277;
  3. In the form designer, double-click the form.

    This opens the code page for the form and creates the method that handles the Load event for the form.

  4. In the Load event handler, copy and paste the following code.

    // Initialize the object context.
    context = new AdventureWorksEntities();
        // Create a query for orders and related items.
        var orderQuery = context.SalesOrderHeaders
            .Where("it.CustomerID = @customerId",
            new ObjectParameter("customerId", customerId))
        // Set the data source of the binding source to the ObjectResult 
        // returned when the query is executed.
        salesOrderHeaderBindingSource.DataSource = 
    catch (EntitySqlException ex)

    This code executes a query that returns a collection of SalesOrderHeader and related SalesOrderDetail objects for a specific customer and binds the collection of SalesOrderHeader objects to the salesOrderHeaderBindingSource.

See Also