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.
If you have recently added the Entity Data Model item, build the project.
On the Data menu, click Add New Data Source.
On the Choose a Data Source Type page, select Object.
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.
Click Finish.
The SalesOrderHeader data source is added to the Data Sources window.
In the Data Sources window, expand the SalesOrderHeader node.
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.
Drag the SalesOrderDetail navigation property to the form.
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.
Open the code page for the form and add the following using statements (Imports in Visual Basic):
using System.Data.Objects;
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;
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.
In the Load event handler, copy and paste the following code.
// Initialize the object context. context = new AdventureWorksEntities(); try { // Create a query for orders and related items. var orderQuery = context.SalesOrderHeaders .Where("it.CustomerID = @customerId", new ObjectParameter("customerId", customerId)) .Include("SalesOrderDetails"); // Set the data source of the binding source to the ObjectResult // returned when the query is executed. salesOrderHeaderBindingSource.DataSource = orderQuery.Execute(MergeOption.AppendOnly); } catch (EntitySqlException ex) { MessageBox.Show(ex.Message); }
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.
How to: Bind Objects to Windows Presentation Foundation Controls
How to: Bind Objects to Windows Form Controls