首页 新闻 搜索 专区 学院

treeview 递归遍历

0
悬赏园豆:50 [已解决问题] 解决于 2009-07-09 08:08

//调用的方法

 private void addprtreeview()
        {
            SqlHelper shp = new SqlHelper();
            string sqlstr = "select * from nodesTB ";
            DataSet ds = new DataSet();
            ds = shp.Query(sqlstr);

            this.treeView1.Nodes.Clear();

           TreeNode tt=new TreeNode();
           LoadTree(tt,ds);

           this.label1.Text = "个人心情多媒体空间站";
        }

//递归方法

 private void LoadTree(TreeNode tn,DataSet ds)
        {
            //加载根节点
            if (tn == null)
            {
                //从数据库获取上级节点编号=0的DataSet
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    TreeNode newtn = new TreeNode();
                    newtn.Text = dr["nodetext"].ToString();
                    newtn.Tag = dr["nodeID"].ToString().Trim();
                    newtn.ContextMenuStrip = this.contextMenuStrip1;
                    tn.Nodes.Add(newtn);

                    LoadTree(newtn, ds);//递归
                }

            }
            //获取当前节点的下一层节点DataSet
            else
            {

                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    TreeNode newtn = new TreeNode();
                    newtn.Text = dr["nodetext"].ToString();
                    newtn.Tag = dr["nodeID"].ToString().Trim();
                    newtn.ContextMenuStrip = this.contextMenuStrip1;
                    tn.Nodes.Add(newtn);

                    LoadTree(newtn, ds);//递归
                   
                }
            }

问题:出现了死循环。我的参考:http://zhidao.baidu.com/question/64884430.html

zcl的主页 zcl | 初学一级 | 园豆:0
提问于:2009-07-08 15:57
< >
分享
最佳答案
0

 private void LoadTree(TreeNode tn,DataSet ds)
        {
            //加载根节点
            if (tn == null)
            {
                //从数据库获取上级节点编号=0的DataSet
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    TreeNode newtn = new TreeNode();
                    newtn.Text = dr["nodetext"].ToString();
                    newtn.Tag = dr["nodeID"].ToString().Trim();
                    newtn.ContextMenuStrip = this.contextMenuStrip1;
                    tn.Nodes.Add(newtn);

                    LoadTree(newtn, ds);//递归 

      这个地方有问题!!虽然你把新节点传进去了,但是你的数据源还是全部节点的数据源,你应该传进去的是当前新节点子节点 的集合!!,不然就是无休止的循环。递归-应该是越来越少的。
                }

            }
            //获取当前节点的下一层节点DataSet
            else
            {

                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    TreeNode newtn = new TreeNode();
                    newtn.Text = dr["nodetext"].ToString();
                    newtn.Tag = dr["nodeID"].ToString().Trim();
                    newtn.ContextMenuStrip = this.contextMenuStrip1;
                    tn.Nodes.Add(newtn);

                    LoadTree(newtn, ds);//递归

      同样的问题!!!!
                    
                }
            }

邢少 | 专家六级 |园豆:10926 | 2009-07-08 18:19
其他回答(1)
0

 private DataTable dt;
    protected void Page_Load(object sender, EventArgs e)
    {
        dt = new MapHelp().GetAreaTreeView("0");
        if (!IsPostBack)
        {
            TreeNode tnode = new TreeNode();
            tnode.Text = "中國";
            tnode.Value = "1";
            tnode.ToolTip = "1";
            HouseTree.Nodes.Add(tnode);//添加根節點
            GetTreeData(tnode);//添加下一層節點
        }
        detailDiv.InnerHtml = detailHidden.Value;
    }
    private void GetTreeData(TreeNode node)
    {
        DataRow[] dr = dt.Select("up_id=1");
        foreach (DataRow mydr in dr)
        {
            TreeNode tnode = new TreeNode();
            tnode.Text = mydr["area_nm"].ToString().Trim();
            tnode.Value = mydr["area_id"].ToString().Trim();
            tnode.ToolTip = tnode.Value;
            node.ChildNodes.Add(tnode);
            //AddHouseTreeChildNodes(dt, tnode);//此句放開則加載所有節點
            //tnode.Expanded = false;
        }
    }

    private void AddHouseTreeChildNodes(DataTable dt ,TreeNode node)
    {
        DataRow[] dr = dt.Select("up_id=" + node.Value);
        foreach (DataRow mydr in dr)
        {
            TreeNode childNode = new TreeNode();
            childNode.Text = mydr["area_nm"].ToString().Trim();
            childNode.Value = mydr["area_id"].ToString().Trim();
            node.ChildNodes.Add(childNode);
            AddHouseTreeChildNodes(dt, childNode);
        }
    }
    protected void HouseTree_SelectedNodeChanged(object sender, EventArgs e)
    {
        detailDiv.InnerHtml = "";
        HouseTree.SelectedNode.ChildNodes.Clear();//清空節點以免重複添加節點
        AddHouseTreeChildNodes(dt, HouseTree.SelectedNode);
        DataTable mydt = new MapHelp().GetHouseInformation(HouseTree.SelectedNode.Value);
        if (dt != null && dt.Rows.Count > 0)
        {
            AddTreeFileData(mydt, HouseTree.SelectedNode);           
            HouseTree.SelectedNode.Expanded = true;
        }
        //明細資料顯示
        DataTable detailTab = new MapHelp().GetHouseInformation(HouseTree.SelectedNode.Value);
        if (detailTab != null && detailTab.Rows.Count > 0)
        {
           
        }
    }
    private void AddTreeFileData(DataTable mydt, TreeNode node)
    {
        foreach (DataRow dr in mydt.Rows)
        {
            TreeNode childNode = new TreeNode();
            childNode.Text = dr["room_nm"].ToString().Trim();
            childNode.Value=dr["room_id"].ToString().Trim();
            childNode.ImageUrl = "../images/page_white_text.png";
            childNode.ToolTip = dr["room_id"].ToString().Trim();
            node.ChildNodes.Add(childNode);
            childNode.SelectAction = TreeNodeSelectAction.None;           
        }
    }

可以參考下上面的代碼,下加載2層,然後點擊那層加載那層,

soarfe | 园豆:146 (初学一级) | 2009-07-08 16:46
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册