Wednesday, August 29, 2012

Treeview dynamically adding parent and Child Nodes in asp.net

       private void PopulateTreeview()
        {
            ObjGroupDO.AccountID =Convert.ToInt64(Session["AccountID"]);
            ObjGroupDO.UEmail = Session["UserName"].ToString();
            this.tvHierarchyView.Nodes.Clear();
            List<GroupDO> objGroupList = Group.SelectAll(ObjGroupDO.AccountID,ObjGroupDO.UEmail);
            foreach (GroupDO hTree in objGroupList)
            {
                GroupDO parentNode = objGroupList.Find(delegate(GroupDO emp) { return emp.GroupID == hTree.ParentGroupID; });
                if (parentNode != null)
                {
                    foreach (TreeNode tn in tvHierarchyView.Nodes)
                    {
                        if (tn.Value == parentNode.GroupID.ToString())
                        {
                            tn.ChildNodes.Add(new TreeNode(hTree.GroupName.ToString(), hTree.GroupID.ToString()));
                        }
                        if (tn.ChildNodes.Count > 0)
                        {
                            foreach (TreeNode ctn in tn.ChildNodes)
                            {
                                RecursiveChild(ctn, parentNode.GroupID.ToString(), hTree);
                            }
                        }
                    }
                }
                else
                {
                    tvHierarchyView.Nodes.Add(new TreeNode(hTree.GroupName, hTree.GroupID.ToString()));
                }
            }

        }
        public void RecursiveChild(TreeNode tn, string searchValue, GroupDO hTree)
        {
            if (tn.Value == searchValue)
            {
                tn.ChildNodes.Add(new TreeNode(hTree.GroupName.ToString(), hTree.GroupID.ToString()));
            }
            if (tn.ChildNodes.Count > 0)
            {
                foreach (TreeNode ctn in tn.ChildNodes)
                {
                    RecursiveChild(ctn, searchValue, hTree);
                }
            }
        }

No comments:

Post a Comment