65.9K
CodeProject is changing. Read more.
Home

Master Detail Datagridview in C#

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.98/5 (24 votes)

Nov 17, 2017

CPOL

1 min read

viewsIcon

81218

downloadIcon

4491

This is an alternative for "Master Detail Datagridview"

Credits

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

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