Click here to Skip to main content
15,895,709 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I've come across this problem several times when trying to bind objects to data grids or comboboxes and I haven't found a solution I'm very happy with so I figured I'd throw the question out there.

Imagine I have a List of Parents, each parent object contains a List of Children. Lets say I want to list all the Parents in a Data Grid and then list all the Children in a different Data Grid. I have a List of Parents so databinding that is easy. However for the Children I don't have one giant list of all of them, they a bunch of Lists that are stored in each of their parents. Is there a way to databind to a structure like this that I am not aware of?

I know one solution is to have separate complete Lists of Parents and Children and create a ParentID in each Child object to allow me to join two Lists in a Linq statement if I need to select the Children of a single Parent. But logically it just makes so much sense to put a List of each Parent's Children in the Parent object. For everything other than binding, this works great. I'm hoping there is a way this can be done.

EDIT:

To clarify, I am talking about the below type of structure

C#
public class Parents
{
    public string Spouse1Name { get; set; }
    public string Spouse2Name { get; set; }
    public List<Child> Children { get; set; }
}

public class Child
{
    public string Name { get; set; }
}


I then have List<Parents> Parents in my ModelView that can be used to databind to all the Parents. Aside from creating a whole other List<child> object to contain the children, is there any way to databind to all the children of all each of the parents (not just a single parent)?
Posted
Updated 19-Mar-15 7:40am
v4

1 solution

Quote:
But logically it just makes so much sense to put a List of each Parent's Children in the Parent object. For everything other than binding, this works great

You are correct here. And the approach is also quick and easy. Few Pointers for you to start.

1) You need two Classes. 1 Child and 1 Parent. Your Parent Class should contain a Child Class List object so that you can have all data of one parent object.

ex:
C#
class ChildData
    {
        public int CClassID { get; set; }
        public String CClassValue { get; set; }
    }

    class ParentData
    {
        public int ParentClassID { get; set; }
        public String ParentClassName { get; set; }
        public List<childdata> ChildList { get; set; }
    } 


Now your ParentData has all the info. Bind ParentClassID and ParentClassName to First DataGridView or ComboBox and based on the Index, Get the ChildData Element from ParentData and Bind it to your Second DataGridView or ComboBox.

If you have more specific Data Structure, Approach is same but you may need to Change your logic a little based on your requirement.

AVK
 
Share this answer
 
v2
Comments
MrGlass3 18-Mar-15 17:20pm    
But what if I wanted to display all Parents in one and all Children in the other - not just the children of the selected ParentData?
Dave Kreskowiak 19-Mar-15 15:33pm    
You'd need to put all of the children into a separate collection. You cannot bind any control to anything other than a single collection of items.
MrGlass3 19-Mar-15 16:01pm    
That was my understanding, I was hoping I wasn't aware of a trick that would let me do this.

Would I design the Parent and Child objects to have a Parent.ID and Child.ParentID and whenever I wanted to get the children of a specific parent then just perform a Linq query that returns those children? It just seems like a lot of querying going this route just to make it bindable, vs. very simple and straight forward having the Children within the Parent object. But is this how it is typically handled?
Dave Kreskowiak 19-Mar-15 16:03pm    
Typically, yes.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900