Solved

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

Posted on 2014-04-04
6
1,870 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 63

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 63

Accepted Solution

by:
Fernando Soto earned 500 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 63

Expert Comment

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

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

630 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