Click here to Skip to main content
15,885,216 members
Articles / Programming Languages / C#
Alternative
Tip/Trick

Master Detail Datagridview in C#

Rate me:
Please Sign up or sign in to vote.
4.98/5 (25 votes)
18 May 2020CPOL1 min read 78.2K   4.4K   69   20
This is an alternative for "Master Detail Datagridview"
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 my kind of approach on how to display Master-Detail data views inside a single datagridview control. The typical layout we need is shown in a screenshot and the full code overview has been provided.

Credits

This tip is the C# translate version of Master Detail Datagridview. Review the original article for further details.

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 datagridview, expanding a child datagridview will occupy the whole control which will give you not a good layout datagridviews to your data as needed for data validation. The typical layout we need is what we could see in the attached screenshot.

Improvements

  • The grids are populated with List<> objects.
  • The grids are fully editable.
  • The grids show a tooltip based on description attribute of every property of the class.
  • Fixed an issue that prevents moving through the child grid with arrow keys.

Using the Code

In this sample demo, I used a single List<> collection of objects.

Follow these very simple steps:

  1. Declare a variable referenced to MasterControl:
    C#
    MasterGridView masterGridView1
    
  2. Load data:
    C#
    private void Form1_Load(object sender, EventArgs e)
    {            
          masterGridView1.DataSource = Persona.getPersonas();
    }
  3. Set child TabControl on the same hierarchy than parent datagridview:
    C#
    masterGridView1.SetChildTabcontrol();

Full Code Overview

C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

// Developed by: Gani Weecom
// Email:ganiweecom@yahoo.com

// Migration to C# by Telerik
//=======================================================
//Service provided by Telerik (www.telerik.com)
//Conversion powered by NRefactory.
//Twitter: @telerik
//Facebook: facebook.com/telerik
//=======================================================
// and magic touch by kevin@diaz.pe ;)

namespace WindowsFormsApplication3
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            // Important: Set child TabControl on the same hierarchy than parent datagridview
            masterGridView1.SetChildTabcontrol();
        }

        private void Form1_Load(object sender, EventArgs e)
        {            
            masterGridView1.DataSource = Persona.getPersonas();

        }
    }
}

History

  • 13th February, 2020: Initial version

License

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


Written By
Software Developer
Peru Peru
Kevin Diaz is an electrical engineer by UNI in Lima, Peru.

He work developing projects for electrical industry.

Comments and Discussions

 
QuestionCant download Pin
Alberto Viera 202121-Jun-21 10:45
Alberto Viera 202121-Jun-21 10:45 
QuestionError When Setting Wrap Mode = True Pin
alvaro_py26-May-21 19:42
alvaro_py26-May-21 19:42 
QuestionQuestion about: Fixed an issue that prevents moving through the child grid with arrow keys. Pin
Jo_vb.net2-Nov-20 11:49
mvaJo_vb.net2-Nov-20 11:49 
GeneralMy vote of 5 Pin
William Costa Rodrigues20-May-20 19:21
William Costa Rodrigues20-May-20 19:21 
QuestionError position display when scroll gridview Pin
ductuan_itpro13-May-20 20:56
ductuan_itpro13-May-20 20:56 
AnswerRe: Error position display when scroll gridview Pin
kevinuni18-May-20 14:42
kevinuni18-May-20 14:42 
Questionmultidetails show Pin
Marcelus Wallas29-May-19 1:11
Marcelus Wallas29-May-19 1:11 
AnswerRe: multidetails show Pin
kevinuni18-May-20 13:50
kevinuni18-May-20 13:50 
AnswerRe: multidetails show Pin
Jo_vb.net11-Nov-20 3:40
mvaJo_vb.net11-Nov-20 3:40 
QuestionCan't run Pin
Z03niE1-Mar-19 2:22
Z03niE1-Mar-19 2:22 
AnswerRe: Can't run Pin
kevinuni18-May-20 14:43
kevinuni18-May-20 14:43 
QuestionFile download error Pin
Member 1414678512-Feb-19 2:25
Member 1414678512-Feb-19 2:25 
AnswerRe: File download error Pin
OriginalGriff12-Feb-19 2:30
mveOriginalGriff12-Feb-19 2:30 
Question[My vote of 1] Not Working Pin
Yves29-Jan-19 4:58
Yves29-Jan-19 4:58 
AnswerRe: [My vote of 1] Not Working Pin
pt14011-Sep-19 21:39
pt14011-Sep-19 21:39 
QuestionGot a problem opening the C# version Pin
Member 115604910-Aug-18 5:47
Member 115604910-Aug-18 5:47 
AnswerRe: Got a problem opening the C# version Pin
pt14011-Sep-19 21:37
pt14011-Sep-19 21:37 
AnswerRe: Got a problem opening the C# version Pin
Heriberto Lugo16-Jan-23 21:57
Heriberto Lugo16-Jan-23 21:57 
QuestionHow to use it in a thread? Pin
quicksilver17ro15-Feb-18 1:22
quicksilver17ro15-Feb-18 1:22 
QuestionMaster Detail Datagridview in C# Pin
JeffArm28-Nov-17 10:27
JeffArm28-Nov-17 10:27 

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.