You can bind an ObjectQuery to a DataGridView control on a Windows form in a few lines of code. This sample demonstrates how to create the ObjectQuery and assign the ObjectQuery to the DataSource property of a DataGridView control. For more information about this sample, see the Adventure Works Data Binding sample topics in the Entity Framework documentation.



This sample uses the following features of the Entity Framework.


Before running this sample, make sure the following software is installed:

Building the Sample

Use the following procedure to build the sample.

To build the AdventureWorks Data Binding sample application

  1. (Optional) If the AdventureWorks sample database is not already present in your SQL Server instance, download and install it from Sample Databases for Microsoft SQL Server 2005 SP2.

  2. Open the AdWksSalesWinDataBind solution file in Visual Studio.

  3. (Optional) If you created the AdventureWorks database in an instance of SQL Server other than the default instance on the local machine, specify the correct SQL Server instance by modifying the Data Source in the AdventureworksEntities connection string in the App.Config file of the AdWksSalesWinDataBind project.

  4. Build the project.

Running the Sample

Use the following procedure to run the sample.

To run the AdventureWorks Data Binding application

  1. Run the application.

  2. Enter a SalesOrderHeaderID value in the Order Number text box. Use a number between 43659 and 75123, and click the Get Sales Order button.

  3. In the Sales Order Details data grid, modify the number in the Quantity column, and press Enter.

    Line and order totals are updated by business logic on the client.

  4. Click the Add Detail to Order button.

  5. When the Add SalesOrderDetail dialog appears, click to select a Product from the data grid.

  6. Enter a quantity and optional discount, which must be a value between 0 and 1.

  7. Click Create Order. A new order is created and added to the Sales Order Details data grid.

Removing the Sample

Use the following procedure to remove the AdventureWorks Data Binding sample.

To remove the AdventureWorks Data Binding application

  1. Delete the project directory and contents.

  2. (Optional) Drop the AdventureWorks database from the instance of SQL Server.

See Also

Other Resources

ADO.NET Entity Framework
Entity Data Model
Binding Objects to Controls