Using O/R Designer to Bind a Windows Form in LINQ



In this article I will show you how to bind a Windows Form with an Entity created by the O/R designer. Essentially we are going to create a DataSource using a generated article of O/R designer.

I assume that,

  1. You have created a Windows Forms application project.
  2. You have created a DataContext and Entity class using LINQ to SQL class. Imagine you have dragged and dropped a Person class onto the O/R designer from School database. The Dbml file is looking more or less like below.

    FormLinq1.gif

Follow the following steps to bind the Windows Form:
  1. Click on Data from the top menu and choose "Add New Data Source..."

    FormLinq2.gif
     
  2. Select Object as Data Source type

    FormLinq3.gif
     
  3. Choose Data object. Select Person from listed objects and click Finish

    FormLinq4.gif
     
  4. Navigate to Windows Form and open design surface and again click on Data in top menu and choose Show Data Sources

    FormLinq5.gif
     
  5. Now you will get Person in Data Sources listed. Select and drag on design surface.

    FormLinq6.gif

    At the bottom of the designer you will see that a Binding Data Source was created:

    FormLinq7.gif
     
  6. Now open the code for the Form class and add the following code. Since you know to populate the data you need to set the Data Source of Binding Source as DataContext .

    FormLinq8.gif
     
  7. To save the modification and add new rows, open the Properties of the Save button of the navigation bar:

    FormLinq9.gif

    Then in the Properties Windows set Enabled to true.

    FormLinq10.gif
     
  8. In the event of the Save button write the following code:

    FormLinq11.gif

Press F5 to run the application.

FormLinq12.gif

Now you should able to perform all the operations.

I hope this article was useful. Thanks for reading.