Here we will learn how to create a windows
forms Data source from an Entity in an Entity Data Model.
For Example : We have a database whose
name is 'mcn' and a window form in .Net Framework3.5.\ and how to install
database on the same page.
We have to follow some steps which as given below :
Create a new window form application - Go in visual studio, select
file>new>project> in main menu.
- Choose the windows forms application. Click Ok .The solution is
created.
Adding an Entity Data Model
- In the solution Explorer ,right click your application and select
>Add>New Item. Form Visual Studio installed templates select Ado.net
entity data model. Click Add.
- See the entity data model wizard to show the entity data model from the mcn database.
- Select the connection we have to made earlier to the mcn database. if not to be ready done so, we can create the new connection at that time by clicking New connection.
- Click Next.
- The Entity data model wizard connects to the database .See the
tree structure of the database. select the object we would like to include in
our model. If we had created views and stored routines these will be displayed
along with any tables. here we have to need to select the tables.
Click Finish to create the model and exit the wizard.
- Visual Studio will generate the model and then display it.
- From the visual studio main menu select Build, Build Solution.
Adding a new data source
Now add a new data source in project and see how it can be used to read and
write database.
- In visual studio main menu select Data, Add New Data Source . see the Data Source Configuration Wizard.
- Select the Object icon .Click Next.
- Select the object to bind .Expand the tree . Select the persons
table and click Next.
- The wizard will confirm that the city object is to be added. Click Finish.
- The persons object will be display in data source panel.
Using the Data Source in a windows Form
To use the data source in a window form follow the steps:
- In the data source panel >select data source and drag & drop it on to the
form designer.By default the data source object will be added as a data
grid view control.it bind the personsBindingsource and personsBindingNavigator.
- Save and rebuild the solution before continuing.
Adding code to populate the data grid view
The data grid view control will be populated with data from the persons database
tables.
- Double click on the form to access it's code
- Add code to instatiate the entity data model container object and retrieve
data from the database
- Save and rebuild the solution
- Run the solution
Adding Code to Save Changes to the Database
Now add code to enable to save changes to the database. The binding source
component ensures that changes made in the data grid view control are also made
to the entity classes bound to it.
- In the form designer, click the save icon in the form toolbar and ensure that
its enabled property is set to true.
- Double click to the save icon in the form toolbar to display its code.
- Need to add code to ensure that data is
saved to the database when the save button is clicked in the application.
Resources
Here are some useful related resources: