tryingtolearn
Member
- Joined
- Mar 13, 2018
- Messages
- 12
- Programming Experience
- Beginner
Hello,
This should be fairly simple, I think I am close. I have a custom object called "response", which I created a list of:
Each "response" object in the lstResponses has two properties: Key and Name. Because there may be two objects in the list with the same key, I decided to group them like so:
So far, so good. But I need help getting the above into a treeview. This is what I have:
That second for loop is where I am stuck. I don't know trees enough to know how to add the child.
Thanks in advance,
Rob
This should be fairly simple, I think I am close. I have a custom object called "response", which I created a list of:
C#:
[COLOR=blue]public[/COLOR] [COLOR=#2b91af]List[/COLOR]<[COLOR=#2b91af]response[/COLOR]> lstResponses = [COLOR=blue]new[/COLOR] [COLOR=#2b91af]List[/COLOR]<[COLOR=#2b91af]response[/COLOR]>();
Each "response" object in the lstResponses has two properties: Key and Name. Because there may be two objects in the list with the same key, I decided to group them like so:
C#:
[COLOR=blue]var[/COLOR] groupedResponses = lstResponses
.GroupBy(u => u.key)
.Select(grp => grp.ToList())
.ToList();
So far, so good. But I need help getting the above into a treeview. This is what I have:
C#:
[COLOR=blue]foreach[/COLOR] ([COLOR=blue]var[/COLOR] root [COLOR=blue]in[/COLOR] groupedResponses)
{
//Add ROOT to treeview
[COLOR=#2b91af]TreeNode[/COLOR] key = [COLOR=blue]new[/COLOR] [COLOR=#2b91af]TreeNode[/COLOR](root[0].key);
treeResponses.Nodes.Add(key);
[COLOR=blue]for[/COLOR] ([COLOR=blue]int[/COLOR] i = 0; i < root.Count; i++)
{
childToAdd=root[i].Name;
// How [COLOR=blue]do[/COLOR] i add the child node here?
}
}
That second for loop is where I am stuck. I don't know trees enough to know how to add the child.
Thanks in advance,
Rob