0

I'm playing with Linq-SQL and would like to display my data in a TreeView on a form. I'm also using .net 3.5, if that matters.

Now, my question - is there a better way to populate this treeview? The way I'm doing it now is like this (psuedo):

for each order
{
  OrderNode = new TreeViewNode

  for each product in order
  {
     ProductNode = new TreeViewNode
     OrderNode.Add(ProductNode)
  }

  OrdersTreeView.Add(OrderNode)
}

Thanks in advance!

1 Answer 1

2

Here is a rough way to create the nodes given recursion (in lovely mashed up half-psuedo)

private void CreateNodeAndInvestigateChildrenOfNode(HierarchyData data)
    {
        //does this node have children???
        if (data.HasChildren)
        {
            //get children
            IEnumerable<ChildRecord> childUsers = GetChildRecordsForData(data);
            foreach (child in childUsers)
            {
      HierarchyData newNode = new HierarchyData ();
                newNode.ParentNode = data;
                newNode.ThisData = child;
                data.ChildNodes.Add(newNode);

                CreateNodeAndInvestigateChildrenOfNode(newNode);
            }
        }
    }

Find your root node and call the method.

If you use the interfaces IHierarchyData and IHierarchicalEnumerable and build the nodes with classes implementing these the treenode will accept this as a direct data source.

Sign up to request clarification or add additional context in comments.

1 Comment

Hey, that's a better way than what I was doing! Thanks :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.