Click here to Skip to main content
13,626,902 members
Click here to Skip to main content
Add your own
alternative version

Stats

11.2K views
840 downloads
25 bookmarked
Posted 16 Nov 2017
Licenced CPOL

Master Detail Datagridview in C#

, 22 Jun 2018
Rate this:
Please Sign up or sign in to vote.
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 but expanding a chil 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 screenshot attached.

Improvements

  • The grids are ppopulated with List<> objects
  • The grids be fully editable
  • The grids show a tooltip based on description attribute of every property of the class
  • Fixed and issue that prevent move 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();

        }

    }
}

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.

You may also be interested in...

Comments and Discussions

 
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.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web02 | 2.8.180712.1 | Last Updated 23 Jun 2018
Article Copyright 2017 by kevinuni
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid