Solved

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

Posted on 2014-04-04
6
1,724 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
  • 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

680 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