Click here to Skip to main content
15,886,720 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.5K   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

 
GeneralRe: The contents of the c # CAN USE? Pin
Ahmet Bayar21-Oct-15 23:30
professionalAhmet Bayar21-Oct-15 23:30 
QuestionHelp me change the awesome control to C# code or DLL can use in C# Pin
Nguyễn Văn Lượng26-Apr-15 13:14
Nguyễn Văn Lượng26-Apr-15 13:14 
AnswerRe: Help me change the awesome control to C# code or DLL can use in C# Pin
ganiweecom27-Apr-15 13:45
ganiweecom27-Apr-15 13:45 
GeneralRe: Help me change the awesome control to C# code or DLL can use in C# Pin
Nguyễn Văn Lượng27-Apr-15 20:13
Nguyễn Văn Lượng27-Apr-15 20:13 
QuestionNeed help to expand further Pin
Biswojit Kumar Sahu2-Apr-15 12:19
professionalBiswojit Kumar Sahu2-Apr-15 12:19 
AnswerRe: Need help to expand further Pin
ganiweecom2-Apr-15 15:24
ganiweecom2-Apr-15 15:24 
GeneralRe: Need help to expand further Pin
Biswojit Kumar Sahu5-Apr-15 14:51
professionalBiswojit Kumar Sahu5-Apr-15 14:51 
GeneralRe: Need help to expand further Pin
ganiweecom12-Apr-15 14:58
ganiweecom12-Apr-15 14:58 
Pls email me your concern: ganiweecom@yahoo.com

Questionquestions Pin
Member 1148562127-Feb-15 16:03
Member 1148562127-Feb-15 16:03 
AnswerRe: questions Pin
ganiweecom27-Feb-15 16:50
ganiweecom27-Feb-15 16:50 
GeneralRe: questions Pin
Member 1148562127-Feb-15 17:11
Member 1148562127-Feb-15 17:11 
GeneralRe: questions Pin
ganiweecom27-Feb-15 17:26
ganiweecom27-Feb-15 17:26 
Question2 Questions Pin
Member 1148562127-Feb-15 14:31
Member 1148562127-Feb-15 14:31 
AnswerRe: 2 Questions Pin
ganiweecom27-Feb-15 14:52
ganiweecom27-Feb-15 14:52 
GeneralMy vote of 5 Pin
ganiweecom5-Nov-14 23:32
ganiweecom5-Nov-14 23:32 
GeneralMy vote of 5 Pin
Heriberto Lugo2-Nov-14 19:08
Heriberto Lugo2-Nov-14 19:08 
SuggestionRemarks Pin
ganiweecom2-Nov-14 13:15
ganiweecom2-Nov-14 13:15 
QuestionHow to send bulk sms by excelsheet ? Pin
Neha Sharma1-Nov-14 1:41
Neha Sharma1-Nov-14 1:41 

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.