C# Treeview - 生成没有重复节点数的树

发布于 2024-10-30 08:30:49 字数 1963 浏览 3 评论 0原文

已经有一些代码即将完成,需要进行一些最后的调整。

这是我的预期输出:

ParentNode1
|----ChildNode1 (3)
|----ChildNode2 (2)
|----ChildNode3
ParentNode2
|----ChildNode4 (2)
|----ChildNode5

ChildNodes 3 和 5 的节点只有一个“条目”,我从其中提取数据(数据行)。

 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;
               }
        }
  }

为了详细说明我上面的逻辑,如果父节点为 Null,它将添加 1 个子节点(临时节点)。

从这里开始,它总是会击中其他地方。我是 Foreach 的新手,因为从未使用过 Loopnode :\

命中 for (在 foreach 中)并将临时节点与列表中的每个节点进行比较,如果发现重复节点,它将名称从“ChildNode4”修改为“ChildNode4 (2)”为找到的每个重复项添加 1(例如,从“ChildNode4 (2)”到“ChildNode4 (3)”等)

如果在 For 循环中未找到重复项,则索引不会更改,并且从而进入最后的if并将其添加到父节点。

有人能指出我正确的方向吗?也许我需要修复我的 Foreach 或者我错误地使用了我的 GetNodeCount (到目前为止它总是返回 0)。

编辑:从我发布的代码开始,我将始终得到:ChildNode3 (0) 而不是实际数字。不知何故,我需要 GetNodeCount 来实际反映 ChildNode3 的数量,而不实际显示所有 ChildNode3

谢谢!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我的痛♀有谁懂 2024-11-06 08:30:50
//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);
}

根据需要修改。您可以将其设为一个方法,传入父节点作为参数。

//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.

九局 2024-11-06 08:30:49

我建议您将以下代码替换为:

index = contactNode1.GetNodeCount(false);

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

如果

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

这不能回答您的问题,请您发布您的实际结果,以便我们了解问题出在哪里

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文