LINQ to SQL Windows Form Binding Data Step by Step

LINQ to SQL designer in Visual Studio allows us to create mapped class using the drag and drop feature from Server Explorer. Now that is business object which ideally you could use as your datasource and from Visual Studio 2008. So let us have this step by step,

Open your Visual Studio 2008 and create a new Windows Forms application (assume C#).

Now add new item to the project LINQ to SQL (.dbml). Give name NW.dbml.

Open a new Data Connection from your VS 2008’s Server Explorer window.

Point to Northwind database. Drag and drop Customers and Orders table. Since Customer and Order table has one to many relationship the designer will get generated like below,

Dbml_thumb4

Now open a data source from Data menu of your VS 2008. Click on add new Datasource. Add object DataSource.

image

Now click on next, choose Customer.

image

Click next and finish.

Now your datasource window will show both Customer and Order because they are connected through PK/FK.

image

Make Customer as details view

image

Drag Customer to your Windows Forms. Then all the navigator and grid will be created as the RAD (Rapid Application Development).

Now you also drag and drop Orders (this will create DataGrid). Now a add the code to your form_load event.

private void Form1_Load(object sender, EventArgs e)

{

NWDataContext db = new NWDataContext();

this.customerBindingSource.DataSource = db.Customers;

}

 

That's all.

Final look,

image

Namoskar!!!