有这样的代码,我调式时发现
  public void AddTree(int ParentID, TreeNode pNode)
        {
            DataView dvTree = new DataView(ds.Tables[0]);
            //过滤ParentID,得到当前的所有子节点
            dvTree.RowFilter = "[PARENTID] = " + ParentID;///
            foreach (DataRowView Row in dvTree)
            {
                if (pNode == null)
                {    //'̀添加根节点
                    TreeNode Node = treeView1.Nodes.Add(Row["Context"].ToString());
                    AddTree(Int32.Parse(Row["ID"].ToString()), Node);    //再次递归
                }
                else
                {   //添加当前节点的子节点
                    TreeNode Node = pNode.Nodes.Add(Row["Context"].ToString());
                    AddTree(Int32.Parse(Row["ID"].ToString()), Node); //再次递归
                }
            }
        } 我调式时发现如果过虑没有数据,怎么会在跳出VOID过程后又跳到else 后的AddTree(Int32.parse....执行呢??
下面是调用的代码:
 private void Form1_Load(object sender, EventArgs e)
        {
            SqlConnection cn = new SqlConnection(cnstr);
            string cmdstr="select id,context,parentid from tbTree";
            SqlCommand cmd = new SqlCommand(cmdstr, cn);
            SqlDataAdapter ad = new SqlDataAdapter(cmdstr, cn);
            ad.Fill(ds, "tbtree");
            AddTree(0, (TreeNode)null);        }

解决方案 »

  1.   

    是不是结束了某一个节点的AddTree函数,然后又调用了这个节点作为父节点的AddTree函数。应该很正常吧。
      

  2.   

    你的东西是有问题的!
    把DataView dvTree = new DataView(ds.Tables[0]);拿到全局去,效率会高一点,我没有测试,但基本是这样的。
    public void AddTree(int ParentID, TreeNode pNode)
    {
    //过滤ParentID,得到当前的所有子节点
    dvTree.RowFilter = "[PARENTID] = " + ParentID;/// // 判断是否有字节点,如果没有返回
    if(dvTree.Count == 0)
    return; if (pNode == null)
    {    //'̀添加根节点
    pNode = treeView1.Nodes.Add(Row["Context"].ToString());
    } foreach (DataRowView Row in dvTree)
    {
    //添加当前节点的子节点
    TreeNode Node = pNode.Nodes.Add(Row["Context"].ToString()); AddTree(Int32.Parse(Row["ID"].ToString()), Node); //再次递归
    }
    }