Solved

Master detail relations in Linq to sql

Posted on 2009-04-15
1
1,208 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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

762 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

19 Experts available now in Live!

Get 1:1 Help Now