0

I have objects of type "People". Each such object has a property called ManagerID.

On my database the data is something like this:

Id, Name, ManagerId

I need to build a tree on asp.net with the database data. The tree should look like this:

John
Lee
David
  William
  Ernest
    Johan
      Red
    George
    Gabriel
      Albert
      Don
        Gabi
  Marry
Helen
......etc

The people should appear on the tree under the manager, each sub level increase the incrementation.

Now I load the people in a list:

List<People> lst = loadPeople();

How do I transform the list in a tree? Thank you.

3
  • What's in loadPeople();? Do you use EF or any other ORM? If you use just sql you can use a recursive query. Commented Aug 28, 2012 at 9:27
  • loadPeople() is a method that builds the list of objects of type People. The method reads from a database, a web service and do some filters/validations. The point is that I have the list: List<People> witch I need to render as a tree. Commented Aug 28, 2012 at 9:53
  • Each one may have a manager (and only one). Not all the people are managers. Commented Aug 28, 2012 at 10:17

2 Answers 2

1

You could sort list by ManagerID, presumed that people that don't have manager have ManagerID = 0 because is Important that "Top" managers would be added first, and just start to add from top and search for the parent, if there is no parent then person is "Top" manager with no managers for him and add it to the root of the treeview.

Something like this :

protected void Page_Load(object sender, EventArgs e)
{
  List<People> pplList = LoadPeople();

  foreach (People person in pplList.OrderBy(pp => pp.ManagerID))
  {
    IEnumerable<TreeNode> nodes = Extensions.GetItems<TreeNode>(TreeViewPeople.Nodes, item => item.ChildNodes);
    TreeNode parent = nodes.FirstOrDefault(nn => nn.Value.Equals(person.ManagerID.ToString()));         
    TreeNode newNode = new TreeNode(person.Name, person.ID.ToString());
    if (parent == null)
      TreeViewPeople.Nodes.Add(newNode);
    else
      parent.ChildNodes.Add(newNode);          
  }
}

and here is the GetItems method that will return all tree nodes, taken from here : https://stackoverflow.com/a/1815600/351383

  public static class Extensions
  {
    public static IEnumerable<T> GetItems<T>(this IEnumerable collection, Func<T, IEnumerable> selector)
    {
      Stack<IEnumerable<T>> stack = new Stack<IEnumerable<T>>();
      stack.Push(collection.OfType<T>());

      while (stack.Count > 0)
      {
        IEnumerable<T> items = stack.Pop();
        foreach (var item in items)
        {
          yield return item;

          IEnumerable<T> children = selector(item).OfType<T>();
          stack.Push(children);
        }
      }
    }
  }
Sign up to request clarification or add additional context in comments.

5 Comments

Thank you. Works (almost) fine. The only issue I have is that treeView1.Nodes has no Find() method. I use treeView1.FindNode(String id), but this method doesn't use recursive search, only searches for the first level. Before to start writing by myself a recursive search, please tell me what should I do to get access to that method? There is any reference I need? I use framework 4.0.
oh sorry, it's for WinForms tree view, for ASP.NET you have to make recursive function to iterate all nodes, look here : stackoverflow.com/a/3767926/351383
I updated my answer for future readers, now it's for ASP.NET treeview
I know its been a while now but what exactly is "TreeViewPeople.Nodes"?
1

Following below links

http://www.codeproject.com/Articles/10997/Binding-Data-With-TreeView-Control-Asp-net-2-0

http://www.codeproject.com/Articles/24534/How-to-load-data-from-database-to-TreeView

This one is good. Populate TreeView from DataBase

Comments

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.