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.

Zelter Ady
  • 6,266
  • 12
  • 48
  • 75
  • What's in `loadPeople();`? Do you use EF or any other ORM? If you use just sql you can use a recursive query. – Amiram Korach Aug 28 '12 at 09: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 witch I need to render as a tree. – Zelter Ady Aug 28 '12 at 09:53
  • Each one may have a manager (and only one). Not all the people are managers. – Zelter Ady Aug 28 '12 at 10:17

2 Answers2

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);
        }
      }
    }
  }
Community
  • 1
  • 1
Antonio Bakula
  • 20,445
  • 6
  • 75
  • 102
  • 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. – Zelter Ady Aug 28 '12 at 16:19
  • oh sorry, it's for WinForms tree view, for ASP.NET you have to make recursive function to iterate all nodes, look here : http://stackoverflow.com/a/3767926/351383 – Antonio Bakula Aug 29 '12 at 08:52
  • I updated my answer for future readers, now it's for ASP.NET treeview – Antonio Bakula Aug 30 '12 at 15:27
  • I know its been a while now but what exactly is "TreeViewPeople.Nodes"? – Bibek Aryal Dec 21 '16 at 05:49
  • https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.treeview.nodes.aspx – Antonio Bakula Dec 21 '16 at 11:30