This article is
ADO. NET Entity Framework
Series of articlesArticle 3: continue to demonstrate how to use the entity of ADO. NET Entity Framework as the data source of the data control to easily display data records in the Data Control. ADO. the first two articles of NET Entity Framework are as follows: ADO. NET Entity Framework getting started example wizard (with demo program download) ADO. NET Entity Framework getting started example wizard (with demo program download)-Series 2 first, according to ADO. NET Entity Data Model Wizard to create an EDM file. For more information, see ADO. NET Entity Framework getting started example wizard (with demo program download.
1.
Add new data sourceThe procedure is as follows:
Select Add new data source. The data source Configuration Wizard window is displayed. The page is as follows:
Select object as data source type and click Next. On the next page of the wizard, expand the tree node-data model to display all entity classes. Select the entity class for which data source needs to be created, and click Next.
2.
Show Data SourceSelect the data menu/show data sources menu to display data sources in the project.
3.Set the data source created in the previous stepDrag to Windows formForm.By default, a new datagridview control and the navigation Toolbar Control are automatically added to the Windows form. The bindingsource and binding navigation controls are also automatically created, and the preceding two controls are automatically bound to the bindingsource and binding navigation controls. The interface is as follows:
4.
Next
Control display data source
.Add the following code in Windows form: Private void form1_load (Object sender, eventargs e) {northwindentities = new northwindentities (); customersbindingsource. datasource = northwindentities. MERs MERS ;}
The example program running interface is as follows:
5.
Edit and save dataThe bindingsource component ensures that the editing of the datagridview control is updated to the entity class. After editing, you need to save the updated data to the database. Next, adjust the Enable attribute of save in the navigation toolbar to true. Double-click the Save button and add the following code in the SAVE Click Event: Private void customersbindingnavigatorsaveitem_click (Object sender, eventargs e) {northwindentities. savechanges ();} The savechanges () method is responsible for saving the update of the entity class to the database.
Download the source code of this example project.