Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2053
  • Last Modified:

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

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
FrancineTaylor
Asked:
FrancineTaylor
  • 3
  • 2
1 Solution
 
Fernando SotoCommented:
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
 
FrancineTaylorAuthor Commented:
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
 
Fernando SotoCommented:
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
 
FrancineTaylorAuthor Commented:
This is exactly what I was trying to remember how to do, thanks Fernando...
0
 
Fernando SotoCommented:
Not a problem, gladI was able to help.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

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

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now