List directories and subdirectories

2

I have a TreeView component, and a structure in the database that already has in the directories subdirectories table. has a field called directive_parent_guid, if it is null it is because it is root, if not, it is some Id of another directory. So I have the following structure:

Method that returns directories: expecting a parameter that will list only directories with parent guid equal to the last string:

internal List<Diretorio> GetSubDiretorio(String termo)
{
    using (var ctx = new TESTEntities())
    {
        var diretorios = (
            from dir in ctx.DIRETORIO 
            where dir.DIRETORIO_PARENT_GUID == termo
            select new Diretorio()
            {             
                DIRETORIO_GUID = dir.DIRETORIO_GUID,
                XDIRETORIO = dir.XDIRETORIO,                
                REFERENCIA = dir.REFERENCIA
            }
        ).ToList();     
        return diretorios;
    }
}

Treeview padding I've tried:

public void CriarTreeView()
{
    var raiz = DiretorioController.GetInstance().GetSubDiretorio("");
    foreach (var diretorio in raiz )
    {      
        node = new TreeNode(diretorio.XDIRETORIO);
        node.ImageUrl = "~/asstes/img/directory.png";
        TreeView1.Nodes.Add(node);

        foreach (var subDiretorio in DiretorioController.GetInstance().GetSubDiretorio())
        {
              TreeNode nodeSub = new TreeNode(subDiretorio.XDIRETORIO);
              nodeSub.ImageUrl = "~/asstes/img/directory.png";
              node.ChildNodes.Add(nodeSub);
        }
    }
}

In this way it works directories and subdirectories, but it gets manual, if I create a directory within a subdirectory it does not list, I have not been able to apply the correct logic yet. How would you list the subdirectories correctly?

    
asked by anonymous 19.02.2015 / 15:52

1 answer

1

To go through the tree to the leaves, you need to use recursion:

public void CriarTreeView()
{
    var diretorios = DiretorioController.GetInstance().GetSubDiretorio("");
    foreach (var diretorio in diretorios)
    {      
        node = new TreeNode(diretorio.XDIRETORIO);
        node.ImageUrl = "~/asstes/img/directory.png";
        TreeView1.Nodes.Add(node);
        this.CriarTreeView(diretorio, node)
    }
}

public void CriarTreeView(Diretorio diretorio, TreeNode node)
{
    var subDiretorios = DiretorioController.GetInstance().GetSubDiretorio(diretorio.DIRETORIO_GUID);
    foreach (var subDiretorio in subDiretorios)
    {      
        TreeNode subNode = new TreeNode(subDiretorio.XDIRETORIO);
        subNode.ImageUrl = "~/asstes/img/directory.png";
        node.ChildNodes.Add(subNode);
        this.CriarTreeView(subDiretorio, subNode)
    }
}
    
19.02.2015 / 16:05