zoukankan      html  css  js  c++  java
  • TreeView动态构造多级树并实现拖动(收藏)

            private static DataSet objSet = new DataSet();

            //动态构造树-这里支持5级
            public static void SetTrees(TreeView node)
            {
                objSet = Data.GetUnite.GetUnites();
                if (objSet != null)
                {
                    if (objSet.Tables[0].Rows.Count != 0)
                    {
                        //1级
                        for (int i = 0; i < objSet.Tables[0].Rows.Count; i++)
                        {
                            node.Nodes.Add(objSet.Tables[0].Rows[i][1].ToString());
                        }
                        //2级
                        for (int i = 0; i < node.Nodes.Count; i++)
                        {
                            string nodeDir = gettree(node.Nodes[i]);
                            objSet.Clear();
                            objSet = Data.GetUnite.GetUnites1(nodeDir);

                            for (int j = 0; j < objSet.Tables[0].Rows.Count; j++)
                            {
                                node.Nodes[i].Nodes.Add(objSet.Tables[0].Rows[j][1].ToString());
                            }
                            node.Nodes[i].Expand();
                        }
                        //3级
                        for (int i = 0; i < node.Nodes.Count; i++)
                        {
                            for (int k = 0; k < node.Nodes[i].Nodes.Count; k++)
                            {
                                string nodeDir2 = gettree(node.Nodes[i].Nodes[k]);
                                objSet.Clear();
                                objSet = Data.GetUnite.GetUnites1(nodeDir2);
                                for (int j = 0; j < objSet.Tables[0].Rows.Count; j++)
                                {
                                    node.Nodes[i].Nodes[k].Nodes.Add(objSet.Tables[0].Rows[j][1].ToString());
                                    node.Nodes[i].Nodes[k].Expand();
                                }
                            }
                        }
                        //4级
                        for (int i = 0; i < node.Nodes.Count; i++)
                        {
                            for (int k = 0; k < node.Nodes[i].Nodes.Count; k++)
                            {
                                for (int g = 0; g < node.Nodes[i].Nodes[k].Nodes.Count; g++)
                                {
                                    string nodeDir2 = gettree(node.Nodes[i].Nodes[k].Nodes[g]);
                                    objSet.Clear();
                                    objSet = Data.GetUnite.GetUnites1(nodeDir2);
                                    for (int j = 0; j < objSet.Tables[0].Rows.Count; j++)
                                    {
                                        node.Nodes[i].Nodes[k].Nodes[g].Nodes.Add(objSet.Tables[0].Rows[j][1].ToString());
                                        node.Nodes[i].Nodes[k].Nodes[g].Expand();
                                    }
                                }
                            }
                        }
                        //5级
                        for (int i = 0; i < node.Nodes.Count; i++)
                        {
                            for (int k = 0; k < node.Nodes[i].Nodes.Count; k++)
                            {
                                for (int g = 0; g < node.Nodes[i].Nodes[k].Nodes.Count; g++)
                                {
                                    for (int h = 0; h < node.Nodes[i].Nodes[k].Nodes[g].Nodes.Count; h++)
                                    {
                                        string nodeDir2 = gettree(node.Nodes[i].Nodes[k].Nodes[g].Nodes[h]);
                                        objSet.Clear();
                                        objSet = Data.GetUnite.GetUnites1(nodeDir2);
                                        for (int j = 0; j < objSet.Tables[0].Rows.Count; j++)
                                        {
                                            node.Nodes[i].Nodes[k].Nodes[g].Nodes[h].Nodes.Add(objSet.Tables[0].Rows[j][1].ToString());
                                            node.Nodes[i].Nodes[k].Nodes[g].Nodes[h].Expand();
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }

            //利用递归取得节点的路径
            private static string gettree(TreeNode nood)
            {
                if (nood.Parent == null)
                {
                    return nood.Text;
                }
                return Path.Combine(gettree(nood.Parent), nood.Text);
            }

             //树节点拖动操作
            public static void TreeDrop(TreeView treeView3, DragEventArgs e)
            {
                TreeNode newNode;

                if (e.Data.GetDataPresent("System.Windows.Forms.TreeNode", false))
                {

                    Point pt;

                    TreeNode destinationNode;

                    pt = treeView3.PointToClient(new Point(e.X, e.Y));

                    destinationNode = treeView3.GetNodeAt(pt);

                    newNode = (TreeNode)e.Data.GetData("System.Windows.Forms.TreeNode");
                    //最上级不能拖动到下级或者同级
                    if (newNode.Parent == null || newNode.Parent.Parent == null )
                    {
                        //集团总部\第二研究院\1所\安全保卫处\sss
                    }
                    else
                    {
                        string[] FullPath = destinationNode.FullPath.ToString().Split(new string[1] { "\\" }, StringSplitOptions.None);
                       //超过最大级限不能拖动添加
                        if (!destinationNode.Equals(newNode)&& FullPath.Length<5)
                        {

                            //destinationNode.Nodes.Add(newNode.Clone());

                            destinationNode.Nodes.Add((TreeNode)newNode.Clone());


                            destinationNode.Expand();

                            //Remove original node

                            newNode.Remove();

                        }

                        string nodeDIR = gettree(destinationNode);
                        //MessageBox.Show(nodeDIR + "\\" + newNode.Text.ToString());
                        Data.GetUnite.UpdataPernt(newNode.Text.ToString().Trim(), nodeDIR);
                    }
                }
            }

  • 相关阅读:
    ajax的调用
    jqurey的应用
    php数据访问数据层
    php租房子练习
    php投票
    Thinkphp 控制器
    ThinkPHP 框架基础
    php留言板
    php上传文件及头像预览
    php文件操作
  • 原文地址:https://www.cnblogs.com/guoxiaowen/p/1488602.html
Copyright © 2011-2022 走看看