foreach(ControlLib.dbTreeNode node in this.dbTViewDiary.Nodes)
{
if(node.Text.Trim()==rowDay&&node.Parent.Text.Trim()==rowMonth&&    
                                          node.Parent.Parent.Text.Trim()==rowYear)
{ this.dbTViewDiary.SelectedNode=node;
node.Expand(); break;
}
}
这棵树有四个节点,结构大致如下:第一层2006,第二层10,第三层11,12,是一个日期的层次关系,为什么在遍历的时候只能找到最上层的节点,也就是2006,然后循环就结束了,查看this.dbTViewDiary.Nodes.count发现值为1,为什么???

解决方案 »

  1.   

    this.dbTViewDiary.Nodes因为这句话只是返回该节点的子节点,不是所有节点,所以你只能访问一层。如果要全部访问,要用到数据结构中那几种遍历树的方法。比如递归什么的。
      

  2.   

    public static TreeNode[] GetChildNodes(TreeNodeCollection nodes)
    {
    if (nodes.Count <= 0)
    return null;
    ArrayList list = new ArrayList(nodes);
    foreach(TreeNode n in nodes )
    {
    if (n.Nodes.Count <= 0)
    continue;
    TreeNode[] childnodes = GetChildNodes(n.Nodes);
    if ( childnodes != null && childnodes.Length > 0)
    {
    //list.Add(childnodes);
    list.AddRange(childnodes);
    }
    }
    // if (list.Count <= 0)
    // {
    // return null;
    // }
    TreeNode[] nodelist = new TreeNode[list.Count];
    list.CopyTo(nodelist);
    return nodelist;
    }
      

  3.   

    使用wwqna(york)所说 的TreeNodeCollection nodes集合做递归