title: Recursively adding a node into a .NET TreeView
body: <p>I need to create a menu structure taken from a database table which uses an ID and a ParentID and a Rank which is used to determine the order of the nodes.</p>
  
  <pre><code>Root(ID 1, ParentID 0, Rank 1)
    - Node(ID 2, ParentID 1, Rank 1)
      - Node(ID 3, ParentID 2, Rank 1)
        - Node(ID 4, ParentID 3, Rank 1)
        - Node(ID 5, ParentID 3, Rank 2)
      - Node(ID 6, ParentID 2, Rank 2)
      - Node(ID 7, ParentID 2, Rank 3)
    - Node(ID 8, ParentID 1, Rank 2)
      - Node(ID 9, ParentID 8, Rank 1)
      - Node(ID 10, ParentID 8, Rank 2)
  </code></pre>
  
  <p>I've attempted to create a function to iterate through the SQL data and create this tree structure but I'm unsure how to  handle the depth of the addition. I can add the first layer of Nodes by simply checking whether they have a ParentID, I can add the second layer of nodes in the else condition, however i'm unsure how to add the following layers of the heirarchy.</p>
  
  <p><strong>Iterating through the Database:</strong></p>
  
  <pre><code>using (var command = new SqlCommand(_Query, _Connection))
  {
      _Connection.Open();                
      var _Reader = command.ExecuteReader();
      while (_Reader.Read())
      {
          CreateNode((int)_Reader["MenuID"], (int)_Reader["ParentID"], (int)_Reader["Rank"], _Reader["English"].ToString());                    
      }
      _Connection.Close();
  }
  </code></pre>
  
  <p><strong>Creation of the nodes:</strong></p>
  
  <pre><code>private void CreateNode(int id, int parentID, int rank, string text)
  {            
      if(parentID == -1)
      {
          TreeNode _Node = new TreeNode(text, id.ToString());
          Root.Nodes.Add(_Node);
      }
  
      if (parentID != -1)
      {
          foreach (TreeNode _Node in Root.Nodes)
          {                    
              if (_Node.Value == parentID.ToString())
              {
                  _Node.ChildNodes.Add(new TreeNode(text, id.ToString()) { ShowCheckBox = true } );
              }
          }
      }
  }
  </code></pre>
  
  <p><strong>Currently this isn't sorting the nodes by rank</strong></p>
  
  <p>I'd expect the output HTML to be something similar to the following:</p>
  
  <pre><code>&lt;ul id="1"&gt;
      &lt;li&gt;A&lt;/li&gt;
      &lt;li&gt;
          &lt;ul id="2"&gt;
              &lt;li&gt;B&lt;/li&gt;
              &lt;li&gt;
                  &lt;ul id="3"&gt;
                      &lt;li&gt;C&lt;/li&gt;
                      &lt;li&gt;
                          &lt;ul id="4"&gt;
                              &lt;li&gt;D&lt;/li&gt;
                          &lt;/ul&gt;
                      &lt;/li&gt;
                      &lt;li&gt;
                          &lt;ul id="5"&gt;
                              &lt;li&gt;E&lt;/li&gt;
                          &lt;/ul&gt;
                      &lt;/li&gt;
                  &lt;/ul&gt;                
              &lt;/li&gt;
              &lt;li&gt;
                  &lt;ul id="6"&gt;
                      &lt;li&gt;F&lt;/li&gt;
                  &lt;/ul&gt;
              &lt;/li&gt;
              &lt;li&gt;
                  &lt;ul id="7"&gt;
                      &lt;li&gt;G&lt;/li&gt;
                  &lt;/ul&gt;
              &lt;/li&gt;            
          &lt;/ul&gt;
      &lt;/li&gt;        
      &lt;li&gt;
          &lt;ul id="8"&gt;
              &lt;li&gt;H&lt;/li&gt;
              &lt;ul&gt;
                  &lt;li&gt;
                      &lt;ul id="9"&gt;
                          &lt;li&gt;I&lt;/li&gt;
                      &lt;/ul&gt;
                  &lt;/li&gt;
                  &lt;li&gt;
                      &lt;ul id="10"&gt;
                          &lt;li&gt;J&lt;/li&gt;
                      &lt;/ul&gt;
                  &lt;/li&gt;                
              &lt;/ul&gt;
          &lt;/ul&gt;
      &lt;/li&gt;
  &lt;/ul&gt;
  </code></pre>
  
  <p><a href="http://jsfiddle.net/RjE7H/" rel="nofollow">http://jsfiddle.net/RjE7H/</a></p>