C#生成树,绑定TreeView控件。

用递归方法生成树结构。

  /// <summary>
        /// 创建树节点数据
        /// </summary>
        /// <param name="nodes"></param>
        /// <param name="parentID"></param>
        private void CreateTreeView(TreeNodeCollection nodes, string parentID)
        {
            ArrayList al = TableDictionaryDao.GetDataByParentID(parentID);
            foreach(MTableDictionary tableDic in al)
            {
                TreeNode tn = new TreeNode();
                tn.Name = tableDic.ID;
                tn.Text = tableDic.Code + "__" + tableDic.Name;              
                nodes.Add(tn);
                tn.Expand();

                //递归生成一个节点下所有的子节点。
                CreateTreeView(tn.Nodes, tableDic.ID);
            }           
        }
        /// <summary>
        /// 初始化TreeView控件
        /// </summary>
        private void InitTreeView()
        {

            //tvTable为TreeView控件
            tvTable.Nodes.Clear();
            //获取父节点,父节点
            CreateTreeView(tvTable.Nodes, string.Empty);
           
        }

 

posted on 2010-06-21 13:59  rikou  阅读(1013)  评论(0编辑  收藏  举报

导航