private void OnDragDrop(object sender, DragEventArgs e) { IDataObject data = e.Data; TreeListNode childNode = (TreeListNode)data.GetData(typeof(TreeListNode)); if (childNode == null) { return; } TreeList list = (TreeList)sender; DXDragEventArgs args = list.GetDXDragEventArgs(e); DragInsertPosition position = args.DragInsertPosition; TreeListNode parentNode = args.TargetNode; int id; // Get drop position if (position == DragInsertPosition.Before || position == DragInsertPosition.After) { if (parentNode.ParentNode != null) { id = parentNode.ParentNode.Nodes.IndexOf(parentNode); } else { id = list.Nodes.IndexOf(parentNode); } parentNode = parentNode.ParentNode; } else { id = parentNode.Nodes.Count; } BaseEntity childEntity = (BaseEntity)childNode.Tag; BaseEntity parentEntity = (parentNode != null ? (BaseEntity)parentNode.Tag : null); // Remove node from old parent if (childEntity.parent != null) { childEntity.parent.children.Remove(childEntity); } else { entities.Remove(childEntity); } // Add node to new parent if (parentEntity != null) { parentEntity.children.Insert(id, childEntity); } else { entities.Insert(id, childEntity); } childEntity.parent = parentEntity; }
// Add a node to the TreeList when a grid row is dropped. private void treeList_DragDrop(object sender, DragEventArgs e) { // Get extended arguments of the drag event. DXDragEventArgs args = treeList.GetDXDragEventArgs(e); // Get how a node is inserted (as a child, before or after a node, or at the end of the node collection). DragInsertPosition position = args.DragInsertPosition; Person dataRow = e.Data.GetData(typeof(DragAndDropRows.Person)) as Person; if (dataRow == null) { return; } int parentID = (int)treeList.RootValue; // Get the node over which the row is dropped. TreeListNode node = args.TargetNode; // Add a node at the root level. if (node == null) { treeList.AppendNode((new PersonEx(dataRow, parentID)).ToArray(), null); } else { // Add a child node to the target node. if (position == DragInsertPosition.AsChild) { parentID = Convert.ToInt32(node.GetValue("ID")); Object[] targetObject = (new PersonEx(dataRow, parentID)).ToArray(); treeList.AppendNode(targetObject, node); } // Insert a node before the taget node. if (position == DragInsertPosition.Before) { parentID = Convert.ToInt32(node.GetValue("ParentID")); Object[] targetObject = (new PersonEx(dataRow, parentID)).ToArray(); TreeListNode newNode = treeList.AppendNode(targetObject, node.ParentNode); int targetPosition; if (node.ParentNode == null) { targetPosition = treeList.Nodes.IndexOf(node); } else { targetPosition = node.ParentNode.Nodes.IndexOf(node); } treeList.SetNodeIndex(newNode, targetPosition); } node.Expanded = true; } }
private void tlCategory_DragDrop(object sender, DragEventArgs e) { TreeListNode dragNode = (TreeListNode)e.Data.GetData(typeof(TreeListNode)); if (dragNode == null) { return; } TreeList myTree = (TreeList)sender; TreeListHitInfo hitInfo = myTree.CalcHitInfo(myTree.PointToClient(new Point(e.X, e.Y))); if (hitInfo.Node == null) { return; } var targetNode = hitInfo.Node; bool?isPeer = null; DragInsertPosition dip = this.tlCategory.GetDragInsertPosition(); switch (dip) { case DragInsertPosition.AsChild: isPeer = false; break; case DragInsertPosition.Before: case DragInsertPosition.After: isPeer = true; break; case DragInsertPosition.None: default: break; } if (!isPeer.HasValue) { return; } var newParentId = 0; if (isPeer.Value) { newParentId = Convert.ToInt32(targetNode.GetValue(tcParentId)); } else { newParentId = Convert.ToInt32(targetNode.GetValue(tcId)); } var connString = System.Configuration.ConfigurationManager.ConnectionStrings["CTMContext"].ToString(); var commandText = $@"UPDATE DecisionReasonCategory SET ParentId = {newParentId} WHERE Id = {Convert.ToInt32(dragNode.GetValue(tcId))}"; SqlHelper.ExecuteNonQuery(connString, CommandType.Text, commandText); this.tlCategory.ExpandAll(); //BindCategory(); }