递归算法绑定节点

        #region 绑定节点
        /// <summary>
        /// 绑定TreeView_2009-05-19
        /// </summary>
        /// <param name="data"></param>
        public void Bind() {
            //1.判断数据源和控件是否为空
            //2.把数据递归到树上
            if (DataSource == null||TV == null) return;
            foreach (Category ci in DataSource){
                if (ci.ParentCategoryID == ROOTCATEGORYID){
                    TreeNode tn = new TreeNode();
                    tn.Text = ci.CategoryName;
                    tn.Name = ci.CategoryId.ToString();
                    tn.Tag = ci.ParentCategoryID;
                    TV.Nodes.Add(tn);
                    BindChildNode(tn);
                }
            }
        }
        /// <summary>
        /// 绑定子_2009-05-19
        /// </summary>
        /// <param name="tn"></param>
        private void BindChildNode(TreeNode tn) {
            foreach (Category cichild in DataSource){
                if (cichild.ParentCategoryID.ToString() == tn.Name.ToString()) {
                    TreeNode tnchild = new TreeNode();
                    tnchild.Text = cichild.CategoryName;
                    tnchild.Name = cichild.CategoryId.ToString();  
                    tnchild.Tag = cichild.ParentCategoryID;        
                    BindChildNode(tnchild);
                    tn.Nodes.Add(tnchild);
                }
            }
        }
        #endregion
原文地址:https://www.cnblogs.com/chinaniit/p/1501315.html