In this post I will show you, how you could bind a Windows Form with Entity created by O/R designer. Essentially we are going to create a DataSource using generated entity of O/R designer.
I assume that,
1. You have created Windows Application project.
2. You have created Datacontext and entity class using LINQ to SQL class. Imagine you have dragged and dropped Person class on O/R designer from School database. Dbml file is looking more or less like below.
Follow the below steps to bind Windows Form,
1. Click on Data from top menu and choose Add New Data Source
2. Select Object as Data Source type
3. Choose Data object. Select Person from listed objects and click Finish
4. Navigate to Windows Form and open design surface and again click on Data in top menu and choose Show Data Sources
5. Now you will get Person in Data Sources listed. Select and drag on design surface.
In bottom of designer you can see Binding Data Source created
6. Now open the code Form class and add below code. Since you know to populate the data you need to set the Data Source of Binding Source as DataContext .
7. To save modification and add new rows , open Properties of Save button from navigation
Then in Properties Windows set Enabled as true.
8. In event of Save button write below code
Press F5 to run the application
Now you should able to perform all the operation.
I hope this post was useful. Thanks for reading