This is a migrated thread and some comments may be shown as answers.

Master-detail linked grids with ORM

3 Answers 90 Views
Getting Started
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Chris
Top achievements
Rank 1
Chris asked on 21 Jan 2013, 01:24 PM
Hi,

I'm working with ORM for the first time and have worked through the online demonstration to build a simple vb.net Winforms application.

I'm using my own SQL Server DB and would like to get linked master-detail grids (the standard .net DataGridView controls) working.  So my example is a one-to-many relationship between customers and contracts.  The master grid will be customers and the detail grid will show all contracts belonging to that customer.

On my form I have two DGVs and this code:

Private dbContext As New TelerikDBContext()
Private bsCustomers As New BindingSource
Private bsContracts As New BindingSource
 
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
 
Me.bsCustomers.DataSource = dbContext.TblCustomers.ToList()
Me.bsContracts.DataSource = bsCustomers
Me.bsContracts.DataMember = "tblcontracts"
 
Me.DataGridView1.DataSource = bsCustomers
Me.DataGridView2.DataSource = bsContracts
 
End Sub

When I run the application, things look good.  The master grid has all the customers and the detail grid shows the contracts for that customer.  However, when I select another customer in the master grid, nothing is displayed in the contracts - the link seems to be lost.

Am I missing something simple?

Thanks,

Chris.

3 Answers, 1 is accepted

Sort by
0
Doroteya
Telerik team
answered on 23 Jan 2013, 03:01 PM
Hello Chris,

In order to protect the link between the master and the detail grids, I suggest you the following approach:

For the purpose of the example, let's assume that the database you use is Northwind and that you have a WinForms project and an OpenAccess Class Library project. The WinForms project contains the form that will display the grids and the OpenAccess Class Library project contains the entity model.  Let's also assume that the WinForms project is set up to consume the model and your solution is built. 

1) Select the WinForms project and from the Project menu of Visual Studio, select Add New Data Source...
2) From the Choose a Data Source Type page of the wizard, select Object and click Next
3) Expand the node that corresponds to the name of the model project and select the Category object
4) Click Finish
5) Open the Data Source window of Visual Studio and pin it wherever is suitable for you
6) Expand the node so that the Category object is visible
7) Open the form in design view
8) Drag-and-drop the Category object on the form. It will automatically add the necessary grid and the binding source
9) Expand the Category node in the Data Source window and drag-and-drop the Products object. Here again Visual Studio will automatically add the necessary grid and the binding source
10) Open the code behind of the form (F7) and add an Imports clause to the namespace of the model project
11) Modify the code as shown in the snippet:
Public Class Form1
 
    Private dbContext As EntitiesModel
 
    Public Sub Form1()
        InitializeComponent()
    End Sub
 
    Protected Overrides Sub OnLoad(e As EventArgs)
        MyBase.OnLoad(e)
        Me.dbContext = New EntitiesModel()
 
        Me.CategoryBindingSource.DataSource = Me.dbContext.Categories.ToList()
    End Sub
 
    Protected Overrides Sub OnClosing(e As System.ComponentModel.CancelEventArgs)
        MyBase.OnClosing(e)
        Me.dbContext.Dispose()
    End Sub
End Class

12) Build the solution and from now on the functionality that you are looking for will be available

Please find attached a small sample project where the process I described is implemented. In it you can also find how to implement features like adding in the database through the grid.

I hope that helps. If you experience problems with the solution I provided or have additional questions, do not hesitate to get back to us.

 

Greetings,
Doroteya
the Telerik team
Q3'12 SP1 of OpenAccess ORM packs Multi-Table Entities mapping support. Check it out.
0
Chris
Top achievements
Rank 1
answered on 24 Jan 2013, 11:09 AM
Hi,

I'm running Visual Studio 2010 so can't open the sample you've kindly provided.

I've tried drag 'n' dropping the tables from the Data Source section in Visual Studio and using the sample code but I don't get any column headings in the detail grid (let alone any data).

Is it not possible to do all the data linking via code as per my sample?  The master-detail link works when the form is loaded but not once another record is selected in the master grid.

Thanks,

Chris.
0
Doroteya
Telerik team
answered on 28 Jan 2013, 11:38 AM
Hi Chris,

Thank you for your feedback and apologies for the inconvenience caused.

I recreated the scenario in Visual Studio 2010 and you can find the new version of the solution attached at the end.

Regarding your question whether you could protect the link between the grids using the approach you provided in the code snippet, I would recommend you to override the OnLoad() event of the Form class instead of using the Load() event. The point here is that the Load() event occurs once before displaying the form, while the OnLoad() event occurs each time the form changes its view. 

Note, however, that the approach you implement contains code that directly queries the database and once you put that code in the OnLoad() event, it will be executed each time the form changes its view. That will result in slow performance of your application.

To minimize the number of calls to the database, I advise you to consider using a data source. Besides the improvement of the performance, the usage of a data source offers you benefits like: data manipulation support and data caching. Here you can find more details about retrieving data from objects. 

I hope it works for you. If you have any other questions, do not hesitate to get back to us.

 

All the best,
Doroteya
the Telerik team
Q3'12 SP1 of OpenAccess ORM packs Multi-Table Entities mapping support. Check it out.
Tags
Getting Started
Asked by
Chris
Top achievements
Rank 1
Answers by
Doroteya
Telerik team
Chris
Top achievements
Rank 1
Share this question
or