List directories and subdirectories

Asked

Viewed 1,453 times

2

I have a Treeview component, and a structure in the database that already has in the table directories the subdirectories. has a field called director_parent_guid, if it is null it is because it is root, if not, it is some other directory id. So I have the following structure:

Method that returns directories: waiting for a parameter that will list only the directories with Parent guid equal to string passed:

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;
    }
}

Filling in the treeview I 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);
        }
    }
}

This way it is working directories and Subdirectories, however it is manual, if I create a directory inside a subdirectory it does not list, I could not apply the correct logic yet. What it would look like to list the Ubdirectories correctly?

1 answer

1


To traverse 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)
    }
}
  • So it’s creating the third directory, but if I select the first again, it creates below the second always.

  • So now the I problem is another, consider asking a new question.

  • It worked out, thanks

Browser other questions tagged

You are not signed in. Login or sign up in order to post.