0

I have a following List of data and I want display in Tree like fashion. I able to display the with two levels of subfolders (sequentially). But this issue is it can have any number of subfolders. Looks like recursive is the way to go but not sure how to proceed. Any help ?

Sample Data

ItemName ItemID NodeID ParentNodeID NodeName
            0   100   null         Node1
Item1       123 100   null         
Item2       124 100   null         
            0   101   null         Node2   
Item3       125 101   null         
Item4       126 101   null         
            0   103   101          Node3   
Item5       127 103   null         
            0   104   103          Node4
Item6       128 104   null         
            0   105   null          Node5   
Item5       127 105   null    

Expected Output

Node1
     ------Item1
     ------Item2
Node2
     ------Item3
     ------Item4
     ------Node3
                --------Item5
                --------Node4
                            ------Item6
Node5
    -------Item5

What I have tried

Loop through my data
If parentnodeid is null
   find in the list object whether folder exists 
      if so append an item to it and add to the list
      else create folder and add to the list
else
   process sub folder
   if itemid = 0 
      find in the list whether the folder exist
          if so, append to the parent folder and add to the list
   else
      add the item to the list
end if
Indy Guy
  • 51
  • 1
  • 6
  • What have you tried to do so far? What research have you done on displaying a tree as text? What specific problem(s) have you had in implementing your solutions? – Servy Aug 26 '14 at 18:01
  • How do you want to display this as? Graphically on ASPX or just print as text? Also, have you tried anything (code wise)? – But I'm Not A Wrapper Class Aug 26 '14 at 18:01
  • That is strange... so the items in a parent don't have that node as a parent. – Hogan Aug 26 '14 at 18:04
  • @CyberneticTwerkGuruOrc From the list of data, I have the following List dbResults with input data, I loop through each with if conditions and add to my another list. Finally return the list as a json object. Using the json data I will display the tree on the view. – Indy Guy Aug 26 '14 at 18:05

3 Answers3

0

I have a few suggestions to acomplish your goal:

-Recursive is the way to go indeed

-Make your data a bit simpler

-Treat items and nodes the same way

example Data

NodeName NodeID ParentNodeID 
Node1       1   0
Node2       2   0
Node3       3   2
Node4       4   3
Node5       5   0
Item1       6   1            
Item2       7   1   
Item3       8   2   
Item4       9   2    
Item5       10  3
Item6       11  4
Item5       12  5

Heres a code to turn this into a treeview

    SampleDataTable = new DataTable();
    SampleDataTable.Columns.Add("NodeName", typeof(string));
    SampleDataTable.Columns.Add("NodeID", typeof(int));
    SampleDataTable.Columns.Add("ParentNodeID", typeof(int));

    //Insert sample data to this table
    SampleDataTable.Rows.Add("Node1", 1, 0);
    SampleDataTable.Rows.Add("Node2", 2, 0);
    SampleDataTable.Rows.Add("Node3", 3, 2);
    SampleDataTable.Rows.Add("Node4", 4, 3);
    SampleDataTable.Rows.Add("Node5", 5, 0);
    SampleDataTable.Rows.Add("Item1", 6, 1);
    SampleDataTable.Rows.Add("Item2", 7, 1);
    SampleDataTable.Rows.Add("Item3", 8, 2);
    SampleDataTable.Rows.Add("Item4", 9, 2);
    SampleDataTable.Rows.Add("Item5", 10, 3);
    SampleDataTable.Rows.Add("Item6", 11, 4);
    SampleDataTable.Rows.Add("Item7", 12, 5);



   treeView1.Nodes.Clear();
    foreach (DataRow dr in SampleDataTable.Rows)
    {
        if ((int)dr["ParentNodeID"] == 0)
        {
            TreeNode parentNode = new TreeNode();
            parentNode .Text = dr["NodeName"].ToString();
            string value = dr["NodeID"].ToString();
            treeView1.Nodes.Add(parentNode);
            findNode(parentNode, value);
        }
    }

    public void findNode(TreeNode parent, string Id)
    {
        IEnumerable<DataRow> rows = SampleDataTable.Select("ParentNodeID=" + Id);

        foreach (DataRow dr in rows)
        {
            //Create child node
            TreeNode child = new TreeNode();
            child.Text = dr["NodeName"].ToString().Trim();

            parent.Nodes.Add(child);

            //Add more child nodes recursively
            findNode(child, dr["NodeID"].ToString());
        }

    }

Result

Manuvo
  • 748
  • 5
  • 15
  • This does not answer the question, he didn't ask how to turn it into a tree. – Hogan Aug 26 '14 at 18:19
  • @Manuvo Thank you very much it worked fine. Along with your idea I did use some of Hogan too. I really appreciate for your help. – Indy Guy Aug 27 '14 at 20:12
0

If you want to print it as text, i got this method from this post https://stackoverflow.com/a/1649223/2966790

and adjusted it a little

        SampleDataTable = new DataTable();
        SampleDataTable.Columns.Add("NodeName", typeof(string));
        SampleDataTable.Columns.Add("NodeID", typeof(int));
        SampleDataTable.Columns.Add("ParentNodeID", typeof(int));

        //Insert sample data to this table
        SampleDataTable.Rows.Add("Node1", 1, 0);
        SampleDataTable.Rows.Add("Node2", 2, 0);
        SampleDataTable.Rows.Add("Node3", 3, 2);
        SampleDataTable.Rows.Add("Node4", 4, 3);
        SampleDataTable.Rows.Add("Node5", 5, 0);
        SampleDataTable.Rows.Add("Item1", 6, 1);
        SampleDataTable.Rows.Add("Item2", 7, 1);
        SampleDataTable.Rows.Add("Item3", 8, 2);
        SampleDataTable.Rows.Add("Item4", 9, 2);
        SampleDataTable.Rows.Add("Item5", 10, 3);
        SampleDataTable.Rows.Add("Item6", 11, 4);
        SampleDataTable.Rows.Add("Item7", 12, 5);

        Node tree = new Node();

        foreach (DataRow dr in SampleDataTable.Rows)
        {
            if ((int)dr["ParentNodeID"] == 0)
            {
                Node parentNode = new Node();
                parentNode.Name = dr["NodeName"].ToString();
                string value = dr["NodeID"].ToString();
                tree.Children.Add(parentNode);
                findNode(parentNode, value);
            }
        }

        PrintPretty(tree, "   ", true);

functions to populate tree and print it:

    public void findNode(Node parent, string Id)
    {
        IEnumerable<DataRow> rows = SampleDataTable.Select("ParentNodeID=" + Id);

        foreach (DataRow dr in rows)
        {
            //Create child node
            Node child = new Node();
            child.Name = dr["NodeName"].ToString().Trim();

            parent.Children.Add(child);

            //Add more child nodes recursively
            findNode(child, dr["NodeID"].ToString());
        }

    }

    public void PrintPretty(Node node, string indent, bool last)
    {            
        textBox1.AppendText(indent);
        if (last)
        {
            textBox1.AppendText("\\-");
            indent += "  ";
        }
        else
        {
            textBox1.AppendText("|-");
            indent += "| ";
        }
        textBox1.AppendText(node.Name + Environment.NewLine);

        List<Node> Children = node.Children;

        for (int i = 0; i < Children.Count; i++)
            PrintPretty(Children[i], indent, i == Children.Count - 1);
    }

Node Class:

public class Node
{
    public string Name; // method name
    public List<Node> Children;
}

Result:

Result:

Community
  • 1
  • 1
Manuvo
  • 748
  • 5
  • 15
-1

pseudo please use this as a guide:

// setup and call function:
string indent = "";
var toplevel = nodelist.Where(x => x.itemid == 0 && x.parentid == null).ToList();
displaytree(toplevel,indent);


// recursive function
displaytree(List<node> display,string indent)
{
   foreach(item in display)
   {
      Console.WriteLine(indent+item.NodeName);

      foreach(inneritem in nodelist.Where(x => item.nodeid == x.nodeid && x.itemid != null)
      {
          Console.WriteLine(indent+"------"+inneritem.NodeName);
      }
      displaytree(nodelist.Where(x => x.parentid == item.nodeid),indent+"    ");
   }
}
Hogan
  • 69,564
  • 10
  • 76
  • 117
  • Thank you @Hogan, let me try it may work. I will let you know though. – Indy Guy Aug 26 '14 at 19:38
  • Thank you very much it worked fine. Along with your idea I did use some of Manuvo too. I really appreciate for your help. – Indy Guy Aug 27 '14 at 20:10
  • Thank you very much it worked fine. Along with your idea I did use some of Manuvo too. I really appreciate for your help. – Indy Guy Aug 27 '14 at 20:13