简体   繁体   中英

asp.net treeview with folders and file from database

I have a requirement to bind folders and files to the treeview based on table data from data base, i tried lot of solution but failed to meet specific requirement.

Here is what im looking

Top folders 来自数据库的表

I have found one sample code and tried to achieve this

`

 private void PopulateTreeview(DataTable dtFolders)
    {
        tvVendors.Nodes.Clear();
        HierarchyTrees hierarchyTrees = new HierarchyTrees();
        HierarchyTrees.HTree objHTree = null;
                    if (dtFolders.Rows.Count > 0)
                    {
                        foreach (DataRow dr in dtFolders.Rows)
                        {
                            objHTree = new HierarchyTrees.HTree();

                            objHTree.LevelDepth = int.Parse(dr["level"].ToString());
                            objHTree.NodeID = int.Parse(dr["folderid"].ToString());
                            objHTree.UnderParent = int.Parse(dr["parentid"].ToString());
                            objHTree.FIleName=dr["filename"].ToString();
                            objHTree.FilePath=dr["filepath"].ToString();
                            hierarchyTrees.Add(objHTree);
                        }

                    }
        //Iterate through Collections.
        foreach (HierarchyTrees.HTree hTree in hierarchyTrees)
        {
            //Filter the collection HierarchyTrees based on 
            //Iteration as per object Htree Parent ID 
            HierarchyTrees.HTree parentNode = hierarchyTrees.Find
            (delegate(HierarchyTrees.HTree vendor)
            { return vendor.NodeID == hTree.UnderParent; });
            //If parent node has child then populate the leaf node.
            if (parentNode != null)
            {
                foreach (TreeNode tn in tvVendors.Nodes)
                {
                    //If single child then match Node ID with Parent ID
                    if (tn.Value == parentNode.NodeID.ToString())
                    {   
                        tn.ChildNodes.Add(new TreeNode
                        (hTree.NodeDescription.ToString(), hTree.NodeID.ToString()));
                    }

                    //If Node has multiple child ,
                    //recursively traverse through end child or leaf node.
                    if (tn.ChildNodes.Count > 0)
                    {
                        foreach (TreeNode ctn in tn.ChildNodes)
                        {
                            RecursiveChild(ctn, parentNode.NodeID.ToString(), hTree);
                        }
                    }
                }
            }
            //Else add all Node at first level 
            else
            {
                tvVendors.Nodes.Add(new TreeNode
                (hTree.NodeDescription, hTree.NodeID.ToString()));
            }
        }
        tvVendors.ExpandAll();
    }

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

Problem: I'm not able to bind file name to leaf node, if folder does not have any subfolder. bind the files. or if folder has subfolder and file both.bind both at same level. if folder is empty, it has nothing just bind it blank.

Could please test the code provide some solution`

  foreach (TreeNode childnode in GetChildFolderNode(dataRow[ID].ToString(), table))
                    {
                       //Bind all folders
                    }
                    foreach (TreeNode childnode in  GetChildFileNode(dataRow[ID].ToString(), table))
                    {
                        //bind all files
                    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM