Treeview的Node拖动Node节点位置移动并重新排序存储。
关键代码:
-
private void treeView1_DragDrop(object sender, DragEventArgs e)//拖动
-
{
-
//获得拖放中的节点
-
TreeNode moveNode = (TreeNode)e.Data.GetData("System.Windows.Forms.TreeNode");
-
/* 根据鼠标坐标确定要移动到的目标节点 */
-
Point pt;
-
TreeNode targeNode;
-
pt = ((TreeView)(sender)).PointToClient(new Point(e.X, e.Y));
-
targeNode = this.treeView1.GetNodeAt(pt);
-
TreeNode newMoveNode = (TreeNode)moveNode.Clone();
-
bool isNeedToSort = false;
-
/* 要移动及目标node为根节点,添加到根同级;否则放到本结点同级*/
-
if (targeNode.Parent == null && this.mHTRootItem.ContainsKey(moveNode.Name))
-
{
-
treeView1.Nodes.Insert(targeNode.Index, newMoveNode);
-
treeView1.SelectedNode = newMoveNode;
-
moveNode.Remove();
-
isNeedToSort = true;
-
}
-
/* 目标节点为非根节点且*/
-
else if(targeNode.Parent != null && this.mHTItemChild.ContainsKey(targeNode.Name))
-
{
-
string parentFlagKey = string.Empty;
-
Structs.ItemChild childNodeSrc = (Structs.ItemChild)this.mHTItemChild[moveNode.Name];
-
Structs.ItemChild childNodeDest = (Structs.ItemChild)this.mHTItemChild[targeNode.Name];
-
if (childNodeDest.ParentFlagKey == childNodeSrc.ParentFlagKey)
-
{
-
targeNode.Parent.Nodes.Insert(targeNode.Index, newMoveNode);
-
treeView1.SelectedNode = newMoveNode;
-
moveNode.Remove();
-
isNeedToSort = true;
-
}
-
}
-
targeNode.Expand();
-
if (!isNeedToSort)
-
return;
-
/* Resort the field of sequence */
-
string flagKey = string.Empty;
-
if (targeNode.Parent == null) // Root Node
-
{
-
Structs.ItemRoot root;
-
for (int i = 0; i < treeView1.Nodes.Count; i++)
-
{
-
flagKey = treeView1.Nodes[i].Name;
-
root = (Structs.ItemRoot)this.mHTRootItem[flagKey];
-
root.Sequence = i;
-
this.mHTRootItem.Remove(flagKey);
-
DBOperator.UpdateItemRoot(root);
-
this.mHTRootItem.Add(flagKey, root);
-
}
-
}
-
else
-
{
-
Structs.ItemChild child;
-
for (int i = 0; i < targeNode.Parent.Nodes.Count; i++)
-
{
-
flagKey = targeNode.Parent.Nodes[i].Name;
-
child = (Structs.ItemChild)this.mHTItemChild[flagKey];
-
child.Sequence = i;
-
this.mHTItemChild.Remove(flagKey);
-
DBOperator.UpdateItemChild(child);
-
this.mHTItemChild.Add(flagKey, child);
-
}
-
}// end else
-
}
DB及DBAPI设计:
构建根子树的技巧:
-
private void buildTreeView()
-
{
-
this.treeView1.Nodes.Clear();
-
this.mHTRootItem.Clear();
-
this.mHTItemChild.Clear();
-
Structs.ItemRoot[] rootRecords = null;
-
DBOperator.GetItemRoot(out rootRecords);
-
if (rootRecords == null)
-
return;
-
for (int i =0;i {
-
this.mHTRootItem.Add(rootRecords[i].FlagKey, rootRecords[i]);
-
TreeNode newNode = treeView1.Nodes.Add(rootRecords[i].Title);
-
newNode.Name = rootRecords[i].FlagKey.ToString();
-
newNode.ImageIndex = 0;
-
newNode.SelectedImageIndex = 1;
-
AddChild(newNode);
-
}
-
this.treeView1.ExpandAll();
-
}
-
private void AddChild(TreeNode parentNode)
-
{
-
if (parentNode == null)
-
return;
-
string parentFlagKey = parentNode.Name;
-
Structs.ItemChild[] childRecords = null;
-
DBOperator.GetItemChild(out childRecords,parentFlagKey);
-
if (childRecords == null)
-
return;
-
for (int i = 0; i < childRecords.Length;i++)
-
{
-
this.mHTItemChild.Add(childRecords[i].FlagKey, childRecords[i]);
-
TreeNode newNode = parentNode.Nodes.Add(childRecords[i].Title);
-
newNode.Name = childRecords[i].FlagKey;
-
newNode.ImageIndex = 2;
-
newNode.SelectedImageIndex = 3;
-
AddChild(newNode);
-
}
-
}
参考文献:
阅读(4399) | 评论(0) | 转发(0) |