将树视图显示为数据库源中的文件夹资源管理器

问题描述 投票:0回答:1

我的树视图有任何问题。客户需要将根文件夹添加到他的文件管理器中。我尝试使用树视图从他的数据库中填充树。这是他的数据库的一部分: 数据库示例字段

这是我的 C# 代码:

private void PopulateTreeView()
        {
            int idPortafoglioValue = 0;
            if (Request.QueryString["id"] != null)
                idPortafoglioValue = int.Parse(Request.QueryString["id"]);
            string strsql = "SELECT DISTINCT cartella FROM TblPortafogli_Documenti WHERE idportafoglio =" + idPortafoglioValue + creaWhere(); ;
            DataTable dt = Funzioni.OttieniTabellaDalDataBase(strsql);
            List<string> cartelleCreate = new List<string>();
            TreeView.Nodes.Clear(); // Clear existing nodes

            // Crea il nodo radice
            TreeNode rootNode = new TreeNode("Root");
            TreeView.Nodes.Add(rootNode);

            if (dt.Rows.Count == 0)
            {
              
                                return;
            }
            else
            {
                foreach (DataRow dr in dt.Rows)
                {
                    string cartella = dr["cartella"].ToString();
                    string dir = "";

                    if (cartella.Contains("/"))
                        cartella = cartella.Replace('/', '\\');
                    if (cartella.StartsWith("Root\\") == false && cartella != "Root")
                        cartella = "Root\\" + cartella;
                    foreach (string stringa in cartella.Split('\\'))
                    {
                        if (dir == "")
                        {
                            dir = stringa;
                            if (!cartelleCreate.Contains(dir))
                            {
                                rootNode.ChildNodes.Add(new TreeNode(dir, dir));
                                cartelleCreate.Add(dir);
                            }
                        }
                        else
                        {
                            string nodoPadre = dir;
                            dir += "\\" + stringa;
                            if (!cartelleCreate.Contains(dir))
                            {
                                TreeNode nodo = new TreeNode();
                                trovaNodo(nodoPadre, rootNode.ChildNodes, ref nodo);
                                nodo.ChildNodes.Add(new TreeNode(stringa, dir));
                                cartelleCreate.Add(dir);
                            }
                        }
                    }
                }
            }
            TreeView.CollapseAll();
        }

  public void trovaNodo(string value, TreeNodeCollection nodi, ref TreeNode node)
        {
            TreeNode t = new TreeNode();
            foreach (TreeNode nodo in nodi)
            {
                if (nodo.Value == value)
                {
                    node = nodo;
                    return;
                }
                trovaNodo(value, nodo.ChildNodes, ref node);
            }

            return;
        }

我无法编辑他的数据库中的路径列,因为它是一个80GB的数据库..

我尝试添加这一行,但总是有新问题:(例如:保存像 Root xample 这样的路径,我得到一个 Root\Root\Example 文件夹..

 if (cartella.StartsWith("Root\\") == false && cartella != "Root")
                        cartella = "Root\\" + cartella;

你能帮我吗?有什么建议吗?

问候

c# asp.net file directory treeview
1个回答
0
投票

我已经解决了根路径的问题。我希望这会有所帮助:

      private void PopulateTreeView()
    {
        int idPortafoglioValue = 0;
        if (Request.QueryString["id"] != null)
            idPortafoglioValue = int.Parse(Request.QueryString["id"]);
        string strsql = "SELECT DISTINCT cartella FROM TblPortafogli_Documenti WHERE idportafoglio =" + idPortafoglioValue + creaWhere(); ;
        DataTable dt = Funzioni.OttieniTabellaDalDataBase(strsql);
        List<string> cartelleCreate = new List<string>();
        TreeView.Nodes.Clear(); // Clear existing nodes

        // Crea il nodo radice
        TreeNode rootNode = new TreeNode("Root");
        TreeView.Nodes.Add(rootNode);

        if (dt.Rows.Count == 0)
        {
            // Aggiungi un nodo "nessun risultato" sotto il nodo radice se non ci sono risultati
            //TreeNode emptyNode = new TreeNode(GetGlobalResourceObject("Resource", "FrmDocumenti_Portafogli_New_NoFiles").ToString());
            //rootNode.ChildNodes.Add(emptyNode);
            
        }
        else
        {
            foreach (DataRow dr in dt.Rows)
            {
                string cartella = dr["cartella"].ToString();
                string dir = "";

                if (cartella.Contains("/"))
                    cartella = cartella.Replace('/', '\\');
                foreach (string stringa in cartella.Split('\\'))
                {
                    if (dir == "")
                    {
                        if (cartella != "Root")
                        {
                            dir = stringa;
                            if (!cartelleCreate.Contains(dir))
                            {
                                rootNode.ChildNodes.Add(new TreeNode(dir, dir));
                                cartelleCreate.Add(dir);
                            }
                        }
                    }
                    else
                    {
                        if (cartella != "Root")
                        {
                            string nodoPadre = dir;
                            dir += "\\" + stringa;
                            if (!cartelleCreate.Contains(dir))
                            {
                                TreeNode nodo = new TreeNode();
                                trovaNodo(nodoPadre, rootNode.ChildNodes, ref nodo);
                                nodo.ChildNodes.Add(new TreeNode(stringa, dir));
                                cartelleCreate.Add(dir);
                            }
                        }
                    }
                }
            }
        }
        TreeView.CollapseAll();
    }
 public void trovaNodo(string value, TreeNodeCollection nodi, ref TreeNode node)
    {
        TreeNode t = new TreeNode();
        foreach (TreeNode nodo in nodi)
        {
            if (nodo.Value == value)
            {
                node = nodo;
                return;
            }
            trovaNodo(value, nodo.ChildNodes, ref node);
        }

        return;
    }
© www.soinside.com 2019 - 2024. All rights reserved.