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,
Now open a data source from Data menu of your VS 2008. Click on add new Datasource. Add object DataSource.
Now click on next, choose Customer.
Click next and finish.
Now your datasource window will show both Customer and Order because they are connected through PK/FK.
Make Customer as details view
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,
Namoskar!!!
Comments
Anonymous
November 02, 2007
LINQ to SQL designer in Visual Studio allows us to create mapped class using the drag and drop featureAnonymous
November 05, 2007
Welcome to the thirty-fifth edition of Community Convergence. This week we have an interview with C#Anonymous
January 07, 2008
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 DhakaAnonymous
January 29, 2008
Thanks. Just the jump start I needed!!!Anonymous
May 26, 2008
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.Anonymous
June 29, 2009
When i drop the customer and order tables and go back to form1 and then open Menu.. i dont find any "add new datasource" and "Add object DataSource". so how i will do that...Anonymous
June 29, 2009
@vicky, Please use Data Source menu to add/remove data source from Visual Studio.Anonymous
February 02, 2010
HI, That's great. Yes, I got it. Your guide helped me to complete a part of my final year project. That's a big step forward on it. Thanks your very much.Anonymous
February 07, 2010
Great!!! So Clear guideline which helped me so much. Thanks a lot.Anonymous
February 13, 2010
@Little Starfish & Irene, The sole purpose of this BLOG is to help community adopt the latest and greatest Microsoft Technologies. I am happy to see it helped you.Anonymous
May 12, 2010
Thanks man for the quick start!