Treeview的Node拖动Node节点移动

Treeview的Node拖动Node节点位置移动并重新排序存储。

关键代码:


  1. private void treeView1_DragDrop(object sender, DragEventArgs e)//拖动
  2.        {
  3.            //获得拖放中的节点
  4.            TreeNode moveNode = (TreeNode)e.Data.GetData("System.Windows.Forms.TreeNode");
  5.            /* 根据鼠标坐标确定要移动到的目标节点 */
  6.            Point pt;
  7.            TreeNode targeNode;
  8.            pt = ((TreeView)(sender)).PointToClient(new Point(e.X, e.Y));
  9.            targeNode = this.treeView1.GetNodeAt(pt);
  10.            TreeNode newMoveNode = (TreeNode)moveNode.Clone();
  11.            bool isNeedToSort = false;
  12.            /* 要移动及目标node为根节点,添加到根同级;否则放到本结点同级*/
  13.            if (targeNode.Parent == null && this.mHTRootItem.ContainsKey(moveNode.Name))
  14.            {
  15.                treeView1.Nodes.Insert(targeNode.Index, newMoveNode);
  16.                treeView1.SelectedNode = newMoveNode;
  17.                moveNode.Remove();
  18.                isNeedToSort = true;
  19.            }
  20.            /* 目标节点为非根节点且*/
  21.            else if(targeNode.Parent != null && this.mHTItemChild.ContainsKey(targeNode.Name))
  22.            {
  23.                string parentFlagKey = string.Empty;
  24.                Structs.ItemChild childNodeSrc = (Structs.ItemChild)this.mHTItemChild[moveNode.Name];
  25.                Structs.ItemChild childNodeDest = (Structs.ItemChild)this.mHTItemChild[targeNode.Name];
  26.                if (childNodeDest.ParentFlagKey == childNodeSrc.ParentFlagKey)
  27.                {
  28.                    targeNode.Parent.Nodes.Insert(targeNode.Index, newMoveNode);
  29.                    treeView1.SelectedNode = newMoveNode;
  30.                    moveNode.Remove();
  31.                    isNeedToSort = true;
  32.                }
  33.            }
  34.            targeNode.Expand();
  35.            if (!isNeedToSort)
  36.                return;
  37.            /* Resort the field of sequence */
  38.            string flagKey = string.Empty;
  39.            if (targeNode.Parent == null) // Root Node
  40.            {
  41.                Structs.ItemRoot root;
  42.                for (int i = 0; i treeView1.Nodes.Count; i++)
  43.                {
  44.                    flagKey = treeView1.Nodes[i].Name;
  45.                    root = (Structs.ItemRoot)this.mHTRootItem[flagKey];
  46.                    root.Sequence = i;
  47.                    this.mHTRootItem.Remove(flagKey);
  48.                    DBOperator.UpdateItemRoot(root);
  49.                    this.mHTRootItem.Add(flagKey, root);
  50.                }
  51.            }
  52.            else
  53.            {
  54.                Structs.ItemChild child;
  55.                for (int i = 0; i targeNode.Parent.Nodes.Count; i++)
  56.                {
  57.                    flagKey = targeNode.Parent.Nodes[i].Name;
  58.                    child = (Structs.ItemChild)this.mHTItemChild[flagKey];
  59.                    child.Sequence = i;
  60.                    this.mHTItemChild.Remove(flagKey);
  61.                    DBOperator.UpdateItemChild(child);
  62.                    this.mHTItemChild.Add(flagKey, child);
  63.                }
  64.            }// end else
  65.        }


image

image


DB及DBAPI设计:

image


构建根子树的技巧:


  1. private void buildTreeView()
  2. {
  3.     this.treeView1.Nodes.Clear();
  4.     this.mHTRootItem.Clear();
  5.     this.mHTItemChild.Clear();
  6.     Structs.ItemRoot[] rootRecords = null;
  7.     DBOperator.GetItemRoot(out rootRecords);
  8.     if (rootRecords == null)
  9.         return;
  10.     for (int i =0;i {
  11.         this.mHTRootItem.Add(rootRecords[i].FlagKey, rootRecords[i]);
  12.         TreeNode newNode = treeView1.Nodes.Add(rootRecords[i].Title);
  13.         newNode.Name = rootRecords[i].FlagKey.ToString();
  14.         newNode.ImageIndex = 0;
  15.         newNode.SelectedImageIndex = 1;
  16.         AddChild(newNode);
  17.     }
  18.     this.treeView1.ExpandAll();
  19. }
  20. private void AddChild(TreeNode parentNode)
  21. {
  22.     if (parentNode == null)
  23.         return;
  24.     string parentFlagKey = parentNode.Name;
  25.     Structs.ItemChild[] childRecords = null;
  26.     DBOperator.GetItemChild(out childRecords,parentFlagKey);
  27.     if (childRecords == null)
  28.         return;
  29.     for (int i = 0; i childRecords.Length;i++)
  30.     {
  31.         this.mHTItemChild.Add(childRecords[i].FlagKey, childRecords[i]);
  32.         TreeNode newNode = parentNode.Nodes.Add(childRecords[i].Title);
  33.         newNode.Name = childRecords[i].FlagKey;
  34.         newNode.ImageIndex = 2;
  35.         newNode.SelectedImageIndex = 3;
  36.         AddChild(newNode);
  37.     }
  38. }


参考文献:

http://www.th7.cn/Program/net/201212/116043.shtml

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值