Use an Entity Framework Entity as a WinForms Data Source
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.
-
Create a new Windows Application. [For help with this task see MSDN Topic: How to Create a Windows Application Project]
-
Add an Entity Data Model to the project. [For help with this task, see Creating an ADO.NET Entity Framework Entity Data Model]
-
Build the project by clicking Build Solution from the Build menu item.
Adding a new Data Source
-
Select Data item from the menu
-
Choose Add New Data Source, which will start the Data Source Configuration Wizard.
-
In the first page of the wizard, choose Object as your Data Source Type and click Next.
-
On the next page of the wizard, expand the tree and locate the namespace for the Entity Data Model.
-
Expand the Data Model to expose the entity classes.
-
Select the entity that you which to create a Data Source from and click Next.
Show the Data Source Window in the IDE
-
To view the project's Data Sources, select the Data menu item, then Show Data Sources from it's drop down menu.
By default, the Data Source window will be docked and can be shown, hidden or undocked as with any other docked window.
-
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.
Populating the grid with the DataSource's data.
It is still necessary to write the code that trigger's the retrieval of data.
-
Double click the form to access it's code.
-
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;
}
}
}
- 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.
- Enable the save button on the navigation toolbar.
- Click the save button
- In the properties window, change the Enabled property to True.
- Double click the save button to go to it's Click event handler.
- 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();
}