Click here to Skip to main content
13,259,427 members (53,893 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

48.3K views
6.6K downloads
27 bookmarked
Posted 31 Oct 2014

Master Detail Datagridview

, 31 Oct 2014
Rate this:
Please Sign up or sign in to vote.
How to display Master Detail Views in Windows Forms Datagridview

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
    Dim masterDetail As MasterControl
  2. Load data to Dataset
    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
    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

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)

Share

About the Author

ganiweecom
United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

 
SuggestionMigration to C# Pin
kevinuni16-Nov-17 20:34
memberkevinuni16-Nov-17 20:34 
GeneralMy vote of 5 Pin
Ammar Bukhari SEC31-Jul-17 23:07
memberAmmar Bukhari SEC31-Jul-17 23:07 
PraiseRate 5 for the best control Pin
Muhamad Rasyid Ridho10-Jun-17 11:58
memberMuhamad Rasyid Ridho10-Jun-17 11:58 
QuestionConvert to MYSQL Pin
Rodrigo Ribeiro30-Dec-16 1:35
memberRodrigo Ribeiro30-Dec-16 1:35 
QuestionThanks :) Pin
XcentY2-Nov-16 1:09
memberXcentY2-Nov-16 1:09 
QuestionEdit Mode Pin
Member 1263518428-Jul-16 3:40
memberMember 1263518428-Jul-16 3:40 
QuestionRight to left ? Pin
cahill20-Nov-15 8:27
membercahill20-Nov-15 8:27 
QuestionHighlight wanted row when loading. Pin
Member 1198649924-Oct-15 14:00
memberMember 1198649924-Oct-15 14:00 
AnswerRe: Highlight wanted row when loading. Pin
ganiweecom26-Oct-15 21:48
memberganiweecom26-Oct-15 21:48 
QuestionThe contents of the c # CAN USE? Pin
ahmet bayar12-Oct-15 10:30
professionalahmet bayar12-Oct-15 10:30 
AnswerRe: The contents of the c # CAN USE? Pin
ganiweecom21-Oct-15 18:30
memberganiweecom21-Oct-15 18:30 
GeneralRe: The contents of the c # CAN USE? Pin
ahmet bayar21-Oct-15 23:54
professionalahmet bayar21-Oct-15 23:54 
GeneralRe: The contents of the c # CAN USE? Pin
ganiweecom22-Oct-15 0:17
memberganiweecom22-Oct-15 0:17 
GeneralRe: The contents of the c # CAN USE? Pin
ahmet bayar22-Oct-15 0:30
professionalahmet bayar22-Oct-15 0: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 14:14
memberNguyễn Văn Lượng26-Apr-15 14:14 
AnswerRe: Help me change the awesome control to C# code or DLL can use in C# Pin
ganiweecom27-Apr-15 14:45
memberganiweecom27-Apr-15 14: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 21:13
memberNguyễn Văn Lượng27-Apr-15 21:13 
QuestionNeed help to expand further Pin
Biswojit Kumar Sahu2-Apr-15 13:19
professionalBiswojit Kumar Sahu2-Apr-15 13:19 
AnswerRe: Need help to expand further Pin
ganiweecom2-Apr-15 16:24
memberganiweecom2-Apr-15 16:24 
GeneralRe: Need help to expand further Pin
Biswojit Kumar Sahu5-Apr-15 15:51
professionalBiswojit Kumar Sahu5-Apr-15 15:51 
GeneralRe: Need help to expand further Pin
ganiweecom12-Apr-15 15:58
memberganiweecom12-Apr-15 15:58 
Questionquestions Pin
Member 1148562127-Feb-15 17:03
memberMember 1148562127-Feb-15 17:03 
AnswerRe: questions Pin
ganiweecom27-Feb-15 17:50
memberganiweecom27-Feb-15 17:50 
GeneralRe: questions Pin
Member 1148562127-Feb-15 18:11
memberMember 1148562127-Feb-15 18:11 
GeneralRe: questions Pin
ganiweecom27-Feb-15 18:26
memberganiweecom27-Feb-15 18:26 
Question2 Questions Pin
Member 1148562127-Feb-15 15:31
memberMember 1148562127-Feb-15 15:31 
AnswerRe: 2 Questions Pin
ganiweecom27-Feb-15 15:52
memberganiweecom27-Feb-15 15:52 
GeneralMy vote of 5 Pin
ganiweecom6-Nov-14 0:32
memberganiweecom6-Nov-14 0:32 
GeneralMy vote of 5 Pin
Heriberto Lugo2-Nov-14 20:08
memberHeriberto Lugo2-Nov-14 20:08 
SuggestionRemarks Pin
ganiweecom2-Nov-14 14:15
memberganiweecom2-Nov-14 14:15 
QuestionHow to send bulk sms by excelsheet ? Pin
Neha Sharma1-Nov-14 2:41
memberNeha Sharma1-Nov-14 2: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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.171114.1 | Last Updated 31 Oct 2014
Article Copyright 2014 by ganiweecom
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid