Welcome to MSDN Blogs Sign in | Join | Help

Wriju's BLOG

.NET and everything
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!!!

Posted: Friday, November 02, 2007 4:22 PM by wriju

Comments

Noticias externas said:

LINQ to SQL designer in Visual Studio allows us to create mapped class using the drag and drop feature

# November 2, 2007 12:12 PM

Charlie Calvert's Community Blog said:

Welcome to the thirty-fifth edition of Community Convergence. This week we have an interview with C#

# November 5, 2007 2:02 PM

A.K.M. Zakirul Quayum said:

hi,

This is apprent so easy, but as RAD helps us to develop rapidly we need to face problem regarding to customize according to our own choice. So, can you address few links or article where i will get the same type of sample which emphasis on manul code generation or changes after auto generated code.

Thanking in advance.

Zaq

Dhaka

# January 8, 2008 7:32 AM

john said:

Thanks.  Just the jump start I needed!!!

# January 29, 2008 7:27 PM

Turissa said:

This is very good but I want to have both related tables displayed a Details view not just one as a details view and one as a gridview. I want my users to enter data into my form without having to drop into another section.

I personally believe this is impossible (although I was able to do it with MS Access by writing it as a query. I don't know how to do this in Visual Studio)There are absolutley no "How To's" on this problem and feel this is a huge downfall of what forms are supposed to be all about.

# May 26, 2008 12:23 PM
Leave a Comment

(required) 

(required) 

(optional)

(required) 

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Page view tracker