Solved

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

Posted on 2014-04-04
6
1,594 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 62

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 62

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 62

Expert Comment

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

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

746 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now