Use an Entity Framework Entity as a WinForms Data Source

Tuesday, August 12 2008

December 17, 2007 12:58 AM

 

In this tutorial, you will create a Windows Forms Data Source from an Entity in an Entity Data Model.

This tutorial works with a simple entity and does not include navigation properties. Stay tuned for another tutorial that shows off Entity Frameworks navigation properties used for databinding.

  1. Create a new Windows Application. [For help with this task see MSDN Topic: How to Create a Windows Application Project]

  2. Add an Entity Data Model to the project. [For help with this task, see Creating an ADO.NET Entity Framework Entity Data Model]

  3. Build the project by clicking Build Solution from the Build menu item.

Adding a new Data Source

  1. Select Data item from the menu

  2. Choose Add New Data Source, which will start the Data Source Configuration Wizard.

win1

  1. In the first page of the wizard, choose Object as your Data Source Type and click Next.

win2

  1. On the next page of the wizard, expand the tree and locate the namespace for the Entity Data Model.

win3

  1. Expand the Data Model to expose the entity classes.

  2. Select the entity that you which to create a Data Source from and click Next.

win4

Show the Data Source Window in the IDE

  1. To view the project's Data Sources, select the Data menu item, then Show Data Sources from it's drop down menu.

win5

By default, the Data Source window will be docked and can be shown, hidden or undocked as with any other docked window.  

win6

  1. Select the new Data Source which you just created, then drag and drop it onto the Windows Form.

By default, a new DataGridView will be created along with a Navigation toolbar. These controls are bound to the BindingSource and Binding Navigator components that were also created. The BindingSource will provide the hook to the data.

win7

Populating the grid with the DataSource's data.

It is still necessary to write the code that trigger's the retrieval of data.

  1. Double click the form to access it's code.

  2. In the form's Load Event, add code to instantiate the EDM's EntityContainer and retrieve the data.

Note: Bold text represents code that needs to be added.

VB

Imports WindowsApplication1
Public Class Form1
Private aw As AdventureWorksLTEntities
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
aw = New AdventureWorksLTEntities
    CustomerBindingSource.DataSource = aw.Customers.OrderBy(Function
(cust) cust.CompanyName)
End Sub
End Class

C#


namespace WindowsApplication1
{
  public partial class Form1 : Form
  {
AdventureWorksLTEntities aw;

    public Form1()
    {
      InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
      aw=new AdventureWorksLTEntities();
      customerBindingSource.DataSource = aw.Customers;
    }
  }
}

  1. Run the project and you will see that the grid is populated.

Edit and Save Data

The BindingSource component ensures that any edits made to the grid are made to the Entity classes that were bound in the previous step. When the user is finished editing, it is therefore necessary to save the data in the entities back to the data store.

  1. Enable the save button on the navigation toolbar.
    1. Click the save button
    2. In the properties window, change the Enabled property to True.

win8

  1. Double click the save button to go to it's Click event handler.
  2. Add the SaveChanges code to the event.

VB

Private Sub CustomerBindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CustomerBindingNavigatorSaveItem.Click
aw.SaveChanges()
End Sub

C#

private void customerBindingNavigatorSaveItem_Click(object sender, EventArgs e)
{
  aw.SaveChanges();
}