【问题标题】:how to get the second level text from the leaf?如何从叶子中获取二级文本?
【发布时间】:2013-03-12 14:11:22
【问题描述】:

如果我的位置是叶子,如何获得第二个父亲。

例如:

1---->2---->3

1---->2---->3---->4

1---->2---->3---->4---->5

如果我在最后一个节点 (3 or 4 or 5)

如何获取节点(2)的文本?


    foreach (RadTreeNode node in nodeCollection)
    {
        if (node.Nodes.Count == 0)//leaf
        {
            if (!node.Value.TrimEnd().Contains('#'))
            {
                GroupDetails grp_d = new GroupDetails();

                grp_d.Boss_code = 0;
                grp_d.Boss_name = string.Empty;
                if (node.Value.TrimEnd().Split('_').Count() > 2)
                {
                    grp_d.Boss_code = int.Parse(node.Value.TrimEnd().Split('_')[2]);
                    grp_d.Boss_name = node.Value.TrimEnd().Split('_')[3];
                }
                grp_d.Dep_code = int.Parse(node.Value.TrimEnd().Split('_')[0]);
                grp_d.Dep_name = node.Text.TrimEnd() //Here i want to get the second parent text to concatenate  
                grp_d.Dep_year = int.Parse(node.Value.TrimEnd().Split('_')[1]);
                grp_d.Group_id = res;
                grp_det.Add(grp_d);

            }
        }
    }

【问题讨论】:

  • 不好笑,但是在将父节点和当前节点都传递给递归函数时递归查找根节点不是显而易见的解决方案吗?如果“父”节点本身没有父节点,则返回“当前”节点

标签: c# asp.net linq telerik treeview


【解决方案1】:
while( node.parent != null )
 node = node.parent;

return node.child;

假设您只有一个孩子(即节点 (2))

只是建议一种遍历树的合乎逻辑的方式。实施取决于您和您的系统设计!

【讨论】:

    【解决方案2】:
    RadTreeNode node_tmp = new RadTreeNode();
          node_tmp = node;
              while (node_tmp.ParentNode != null)
                    {
                       p_txt = node_tmp.Text.TrimEnd();
                       node_tmp = node_tmp.ParentNode;
    
                    }
    

    【讨论】:

      猜你喜欢
      • 2014-11-21
      • 1970-01-01
      • 2021-09-03
      • 1970-01-01
      • 2023-03-09
      • 1970-01-01
      • 2015-08-25
      • 2020-06-20
      相关资源
      最近更新 更多