Click here to Skip to main content
14,538,686 members

Master Detail Datagridview in C#

Rate this:
5.00 (21 votes)
Please Sign up or sign in to vote.
5.00 (21 votes)
18 May 2020CPOL
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:
    MasterGridView masterGridView1
    
  2. Load data:
    private void Form1_Load(object sender, EventArgs e)
    {            
          masterGridView1.DataSource = Persona.getPersonas();
    }
  3. Set child TabControl on the same hierarchy than parent datagridview:
    masterGridView1.SetChildTabcontrol();

Full Code Overview

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)

Share

About the Author

kevinuni
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

 
GeneralMy vote of 5 Pin
William Costa Rodrigues20-May-20 19:21
MemberWilliam Costa Rodrigues20-May-20 19:21 
QuestionError position display when scroll gridview Pin
ductuan_itpro13-May-20 20:56
Memberductuan_itpro13-May-20 20:56 
AnswerRe: Error position display when scroll gridview Pin
kevinuni18-May-20 14:42
Memberkevinuni18-May-20 14:42 
Questionmultidetails show Pin
Marcelus Wallas29-May-19 1:11
MemberMarcelus Wallas29-May-19 1:11 
AnswerRe: multidetails show Pin
kevinuni18-May-20 13:50
Memberkevinuni18-May-20 13:50 
QuestionCan't run Pin
Z03niE1-Mar-19 2:22
MemberZ03niE1-Mar-19 2:22 
AnswerRe: Can't run Pin
kevinuni18-May-20 14:43
Memberkevinuni18-May-20 14:43 
QuestionFile download error Pin
Member 1414678512-Feb-19 2:25
MemberMember 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
MemberYves29-Jan-19 4:58 
AnswerRe: [My vote of 1] Not Working Pin
pt14011-Sep-19 21:39
Memberpt14011-Sep-19 21:39 
QuestionGot a problem opening the C# version Pin
Member 115604910-Aug-18 5:47
MemberMember 115604910-Aug-18 5:47 
AnswerRe: Got a problem opening the C# version Pin
pt14011-Sep-19 21:37
Memberpt14011-Sep-19 21:37 
QuestionHow to use it in a thread? Pin
quicksilver17ro15-Feb-18 1:22
Memberquicksilver17ro15-Feb-18 1:22 
QuestionMaster Detail Datagridview in C# Pin
JeffArm28-Nov-17 10:27
MemberJeffArm28-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.

Alternative
Tip/Trick
Posted 16 Nov 2017

Tagged as

Stats

50.7K views
2.3K downloads
60 bookmarked