Solved

Master detail relations in Linq to sql

Posted on 2009-04-15
1
1,215 Views
Last Modified: 2013-11-11
how to build that? in datatables I used to have something like this: (code bellow)

msBindSource.DataSource = ds

        msBindSource.DataMember = msDt.TableName

        rgridview.DataSource = msBindSource

        

        detailBindSource.DataSource = msBindSource

       detailBindSource.DataMember = "msRelationStr" -> that is the relation name

Open in new window

0
Comment
Question by:TheSilverBullet
1 Comment
 
LVL 62

Accepted Solution

by:
Fernando Soto earned 400 total points
ID: 24154581
Hi TheSilverBullet;

The best way to do a Master/Detail with Linq to SQL is to let the IDE environment build the underneath plumbing for you. I will use the Microsoft Northwind database to show how to do it.

1. Add a Linq to SQL Class to your project.
2. Once the designer is displayed add the Orders and Order_Details table to the design surface.
3. Save and compile the project.
4. Add a DataSource object to your project By clicking on the Menu -> Data -> Add New DataSource.
5. In the DataSource Window select Object and click on the Next button.
6. Expand the Project Name node and then Expand the node with the Project Name again.
7. Now select the Master table name, in this case it is Orders.
8. Click on the Finish button.
9. Open the DataSource window if it is not already displayed.
10. Drag the Orders table on to the form. This will add one BindingSource component named OrdersBindingSource and a BindingNavigator called OrdersBindingNavigator as well as the Master DataGridView with all plumbing connected for you.
11. Now drag the Order_Details table on to the form from the DataSource window. This will add a BindingSource component called Order_DetailsBindingSource to the form and the Details DataGrridView.
12. Now query the data source as shown in the code snippet below and that is it.
13. Run the project.

Fernando
Public Class Form1
 

    Private Sub Form1_Load(ByVal sender As System.Object, _

        ByVal e As System.EventArgs) Handles MyBase.Load
 

        Dim db As New NorthwindDataContext()
 

        OrdersBindingSource.DataSource = From order In db.Orders _

                                         Select order
 

    End Sub
 

End Class

Open in new window

0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now