?
Solved

How do I bind a list of objects which contain a sublist to a DataGridView

Posted on 2014-04-04
6
Medium Priority
?
1,963 Views
Last Modified: 2014-04-07
My structure is pretty simple.  Something like this:

public class Person {
    public string Name { get; set; }
    public int Age { get; set; }
    public List<Person> Children { get; set; }
}

Open in new window


I want to do a simple binding:

List<Person> people = CreatePeopleList();
gridPeople.DataSource = people;

Open in new window


The grid shows all the Name and Age properties of the top level list of people.  However, the Children property doesn't show up.  I'd like it to show as a sublist in the grid; how can I do that, considering that I don't know how many levels there might be.  Can that be done?
0
Comment
Question by:FrancineTaylor
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 39979548
Hi FrancineTaylor;

To do what you want you will need to DataGrid controls, one for the Person object and the other to show the Children objects. You will need to build a relationship between the two grids. Please see this web page, Creating Master-Details Lists with the Windows Forms DataGrid Control, it will give you the procedure to implement it either through the designer or through code.
0
 
LVL 1

Author Comment

by:FrancineTaylor
ID: 39983615
Hi Fernando,

Thanks for replying, but that wasn't what I was looking for.  I have an object list, not a DataSet.  And I don't want to display in two grids.  I want a single grid, with the list of "children" objects showing as a sub-list below its parent People object.
0
 
LVL 64

Accepted Solution

by:
Fernando Soto earned 2000 total points
ID: 39984025
That functionality is not supported in the DataGridView control. The DataGridView control replaced an older control called DataGrid which is still being supported and does support showing collections within a object on a new line. This is not shown automatically but by clicking the + symbol at the beginning of of a row it opens a line below the row with the collection names and then by clicking the collection opens it within the same grid.

The DataGrid is not shown in the Toolbox by default it must be added to it. This can be done by finding the Tab in the Toolbox called Data. Right click on that Tab and select Choose Items... from the context menu. In the new window that opens select the .Net Framework Components. In the Filter text box type in DataGrid. When the list is displayed place a check mark next to the DataGrid control and then click OK button.

The DataGrid control is now in the Toolbox in the Data Tab. Drag it on to the Form and in code assign the list to DataGrid1.DataSource = YourListOfObjects;. Everything else is connected automatically for you.
0
 
LVL 1

Author Comment

by:FrancineTaylor
ID: 39984546
This is exactly what I was trying to remember how to do, thanks Fernando...
0
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 39984834
Not a problem, gladI was able to help.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

718 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question