Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Master Detail Datagridview

0.00/5 (No votes)
31 Oct 2014 1  
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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here