Click here to Skip to main content
15,881,875 members
Articles / Programming Languages / Visual Basic
Tip/Trick

Master Detail Datagridview

Rate me:
Please Sign up or sign in to vote.
4.89/5 (35 votes)
31 Oct 2014CPOL1 min read 149.2K   13.4K   55   45
How to display Master Detail Views in Windows Forms Datagridview

Image 1

Introduction

Many of us developers come across a problem on how to display Master-Detail views within a single Datagridview control like the third party applications Devexpress.

The native Windows Forms control datagridview doesn’t support Master-Detail views within a single control. By majority approach, this should be done through two separate datagridview controls.

In this tip, I will discuss with you my kind of approach on how to display Master-Detail data views inside a single datagridview control. Although an alternate Datagrid control supports master detail views but expanding a childview will occupy the whole control which will give you not a good layout views to your data as needed for data validation. The typical layout we need is what we could see in the screenshot attached.

Using the Code

In this sample demo, I used a Northwind database.

You will need at least three controls: the panelView which is a panel control to host the MasterControl, the Dataset control containing the data to be displayed which is the NwindDataSet, and the MasterControl which is a Datagridview control that enables Master-Detail views.

Follow these very simple steps:

  1. Declare a variable referenced to MasterControl
    VB.NET
    Dim masterDetail As MasterControl
  2. Load data to Dataset
    VB.NET
    Me.OrderReportsTableAdapter.Fill(Me.NwindDataSet.OrderReports)
    Me.InvoicesTableAdapter.Fill(Me.NwindDataSet.Invoices)
    Me.CustomersTableAdapter.Fill(Me.NwindDataSet.Customers)
  3. Create a master detail data view
    VB.NET
    masterDetail = New MasterControl(NwindDataSet)
    panelView.Controls.Add(masterDetail)
    masterDetail.setParentSource(NwindDataSet.Customers.TableName, "CustomerID")
    masterDetail.childView.Add(NwindDataSet.OrderReports.TableName, "Orders")
    masterDetail.childView.Add(NwindDataSet.Invoices.TableName, "Invoices")

MasterControl Function Description

  • New MasterControl(Dataset) – Create new masterdetail control with a parameter of a dataset value.
  • SetParentSource(TableName,UniqueKey) – Set the table name of the master view and its unique column to child views.
  • childView.Add(TableName,PageCaption) – Set the table name source of the child view to be added and its page caption.

Full Code Overview

VB.NET
Public Class frmMain
    Dim masterDetail As MasterControl
    Sub clearFields()
        panelView.Controls.Clear()
        masterDetail = Nothing
        Refresh()
    End Sub
    Sub loadData()
        clearFields()
        Me.OrderReportsTableAdapter.Fill(Me.NwindDataSet.OrderReports)
        Me.InvoicesTableAdapter.Fill(Me.NwindDataSet.Invoices)
        Me.CustomersTableAdapter.Fill(Me.NwindDataSet.Customers)
        createMasterDetailView()
    End Sub
    Sub createMasterDetailView()
        masterDetail = New MasterControl(NwindDataSet)
        panelView.Controls.Add(masterDetail)
        masterDetail.setParentSource(NwindDataSet.Customers.TableName, "CustomerID")
        masterDetail.childView.Add(NwindDataSet.OrderReports.TableName, "Orders")
        masterDetail.childView.Add(NwindDataSet.Invoices.TableName, "Invoices")
    End Sub
    Private Sub btnLoad_Click(sender As Object, e As EventArgs) Handles btnLoad.Click
        loadData()
    End Sub
End Class
...

Points of Interest

There's nothing more to it. It is particularly useful in situations where you want to display child views like in third party controls.

History

  • 2nd November, 2014: Initial version

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Philippines Philippines
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Questioncant unzip the file Pin
Alberto Viera 202121-Jun-21 10:45
Alberto Viera 202121-Jun-21 10:45 
QuestionHow to add binding navigator Pin
Jo_vb.net30-Oct-20 7:52
mvaJo_vb.net30-Oct-20 7:52 
QuestionIs it allowed to use your code for an article about an extended Master-Detail-Datagridview Pin
Jo_vb.net28-Oct-20 8:57
mvaJo_vb.net28-Oct-20 8:57 
QuestionMessage Removed Pin
23-Oct-20 22:02
mvaJo_vb.net23-Oct-20 22:02 
QuestionHow can I add a Richtext control to this gridview? Pin
Jo_vb.net18-Oct-20 6:58
mvaJo_vb.net18-Oct-20 6:58 
AnswerRe: How can I add a Richtext control to this gridview? Pin
Jo_vb.net22-Oct-20 7:55
mvaJo_vb.net22-Oct-20 7:55 
GeneralNew improvements on my alternative version of master/detail datagridview in C# Pin
kevinuni22-Jun-18 21:55
kevinuni22-Jun-18 21:55 
QuestionHow to use it in a thread? Pin
quicksilver17ro15-Feb-18 1:20
quicksilver17ro15-Feb-18 1:20 
SuggestionMigration to C# Pin
kevinuni16-Nov-17 19:34
kevinuni16-Nov-17 19:34 
GeneralRe: Migration to C# Pin
ganiweecom9-Dec-17 5:24
ganiweecom9-Dec-17 5:24 
GeneralRe: Migration to C# Pin
Heriberto Lugo12-Mar-18 10:59
Heriberto Lugo12-Mar-18 10:59 
GeneralMy vote of 5 Pin
Ammar Bukhari SEC31-Jul-17 22:07
Ammar Bukhari SEC31-Jul-17 22:07 
PraiseRate 5 for the best control Pin
Muhamad Rasyid Ridho10-Jun-17 10:58
Muhamad Rasyid Ridho10-Jun-17 10:58 
QuestionConvert to MYSQL Pin
Rodrigo Ribeiro30-Dec-16 0:35
Rodrigo Ribeiro30-Dec-16 0:35 
QuestionThanks :) Pin
XcentY2-Nov-16 0:09
XcentY2-Nov-16 0:09 
QuestionEdit Mode Pin
Member 1263518428-Jul-16 2:40
Member 1263518428-Jul-16 2:40 
AnswerRe: Edit Mode Pin
Jo_vb.net18-Oct-20 6:31
mvaJo_vb.net18-Oct-20 6:31 
QuestionRight to left ? Pin
cahill20-Nov-15 7:27
cahill20-Nov-15 7:27 
QuestionHighlight wanted row when loading. Pin
Grthigh24-Oct-15 13:00
Grthigh24-Oct-15 13:00 
AnswerRe: Highlight wanted row when loading. Pin
ganiweecom26-Oct-15 20:48
ganiweecom26-Oct-15 20:48 
GeneralRe: Highlight wanted row when loading. Pin
Jo_vb.net21-Oct-20 7:44
mvaJo_vb.net21-Oct-20 7:44 
GeneralRe: Highlight wanted row when loading. Pin
ganiweecom22-Oct-20 2:19
ganiweecom22-Oct-20 2:19 
GeneralRe: Highlight wanted row when loading. Pin
ganiweecom22-Oct-20 2:29
ganiweecom22-Oct-20 2:29 
GeneralRe: Highlight wanted row when loading. Pin
Jo_vb.net22-Oct-20 3:13
mvaJo_vb.net22-Oct-20 3:13 
QuestionThe contents of the c # CAN USE? Pin
Ahmet Bayar12-Oct-15 9:30
professionalAhmet Bayar12-Oct-15 9:30 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.