title: C# Treeview - Generate Tree without Duplicates with Node Count
body: <p>Already have some code that's almost there and is in need of some final tweaks.</p>
  
  <p>Here is my Expected output:</p>
  
  <pre><code>ParentNode1
  |----ChildNode1 (3)
  |----ChildNode2 (2)
  |----ChildNode3
  ParentNode2
  |----ChildNode4 (2)
  |----ChildNode5
  </code></pre>
  
  <p>Node that ChildNodes 3 and 5 only have one "entry" from where I'm pulling my data (Data Row).</p>
  
  <pre><code> dr["Contact"] = drows_cont[0].ItemArray[2].ToString();                                        
   TreeNode temp = new TreeNode();
   temp.Text = (dr["Contact"].ToString());
   temp.Name = temp.Text;           
   if(contactNode1.Nodes.Count == 0)/*node has no children or is null*/
   {
       contactNode1.Nodes.Add(temp);
   }
   else
   {
        int index = 1;
        foreach(TreeNode loopNode in contactNode1.Nodes)
        {                                                                   
             index = 1;
                 for(int i=0; i &lt; contactNode1.Nodes.Count; i++)
                 {
                     if(contactNode1.Nodes[i].Name == temp.Name) //if match found
                     {                                                                  
                          index = contactNode1.GetNodeCount(false);
                          contactNode1.Nodes[i].Text = (dr["Contact"].ToString()) + " " + index;
                     }
                 }
                 if (index &lt;= 1)
                 {                                                        
                     contactNode1.Nodes.Add(temp); //AddToList
                     break;
                 }
          }
    }
  </code></pre>
  
  <p>To elaborate my logic above, if the parent node is Null it will add 1 child node (the temporary node).</p>
  
  <p>From here on it always hits the else. I'm new to Foreach as Loopnode is never used :\</p>
  
  <p>The for (in the foreach) is hit and compares the temp node to each node on the list and if a duplicate node is found it will amend the name from "ChildNode4" to "ChildNode4 (2)" adding 1 for each duplicate found (Ex. from "ChildNode4 (2)" to "ChildNode4 (3)", etc.)</p>
  
  <p>If a duplicate is not found in the For loop the index doesn't change and thus enters the final if and adds it to the parent node.</p>
  
  <p>Could someone point me in the right direction? Maybe I need to fix my Foreach or I'm using my GetNodeCount wrong (so far it's always returning 0).</p>
  
  <p>Edit: As of the code I posted I will always get: ChildNode3 (0) instead of an actual number. Somehow I need GetNodeCount to actually reflect the number of ChildNode3s' without actually showing all of the ChildNode3s </p>
  
  <p>Thanks!</p>