Already have some code that's almost there and is in need of some final tweaks.
Here is my Expected output:
ParentNode1
|----ChildNode1 (3)
|----ChildNode2 (2)
|----ChildNode3
ParentNode2
|----ChildNode4 (2)
|----ChildNode5
Node that ChildNodes 3 and 5 only have one "entry" from where I'm pulling my data (Data Row).
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 < 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 <= 1)
{
contactNode1.Nodes.Add(temp); //AddToList
break;
}
}
}
To elaborate my logic above, if the parent node is Null it will add 1 child node (the temporary node).
From here on it always hits the else. I'm new to Foreach as Loopnode is never used :\
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.)
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.
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).
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
Thanks!