1
votes

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!

2
It's not entirely clear what problem you are encountering. Can you be more specific as to what question you need answering? - Jeff Yates
I suggest saying what is wrong with your code? - TBohnen.jnr
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 - Demasterpl

2 Answers

0
votes

I suggest you replace the code in:

index = contactNode1.GetNodeCount(false);

with

index = contactNode1.Nodes.Count(); //start from 0

or

index = contactNode1.Nodes.Count() + 1; // start from 1

If this does not answer your question please could you post your actual result so that we can understand where the problem is

0
votes
//Add parent
treeView.Nodes.Add(parentNode);
//Loop through every child
foreach(TreeNode childNode in parentNode.Nodes)
{
    int index = 0;
    //Calculate childNode's children
    foreach (TreeNode node in loopNode.Nodes)
    {
        index++;
    }
    string node;
    //If index is 0, dont change text.
    if (index != 0) node = childNode.Text + " (" + index + ")";
    else node = childNode.Text;
    parentNode.Nodes.Add(childNode);
}

Modify as needed. You can make this a method, passing in the parent node as the argument.