procedure TForm1.TVDragDrop(Sender, Source: TObject;
X, Y: Integer);
var
TargetNode, SourceNode: tTreenode;
begin
TargetNode := TV.GetNodeAt(x,y);
SourceNode := TV.Selected;
TV.Items.begin
Update;
try
CopyNodeUnder(TV, SourceNode, TargetNode);
TV.Selected := TargetNode;
finally
TV.Items.EndUpdate;
end;
end;
procedure TForm1.TVDragOver(Sender, Source: TObject;
X, Y: Integer;
State: TDragState;
var Accept: Boolean);
var
TargetNode,SourceNode: tTreeNode;
begin
TargetNode:=TV.GetNodeAt(x,y);
if (Source = Sender) and (TargetNode <> nil) then
begin
Accept:=true;
SourceNode := TV.Selected;
while (TargetNode.Parent <> nil) and (TargetNode <> SourceNode)do
TargetNode := TargetNode.Parent;
if (TargetNode = SourceNode) then
Accept := False;
end else
Accept := False;
end;
procedure TForm1.CopyNodeUnder(const TreeView: TTreeview;
const SourceNode,
TargetNode: tTreeNode);
var
NewNode: tTreeNode;
i: Integer;
TargetParent: Integer;
begin
with dmData.quSQLdo
begin
Close;
SQL.Text := 'select id from department where name like ''' + TargetNode.Text + '''';
Prepared := True;
Open;
TargetParent := Fields[0].AsInteger;
Prepared := False;
Close;
end;
ExecSQL('update department set parent = ' + IntToStr(TargetParent) + ' where name like ''' + SourceNode.Text + '''');
NewNode := TreeView.Items.AddChildFirst(TargetNode, '');
NewNode.Assign(SourceNode);
for i := SourceNode.count - 1do
wnto 0do
CopyNodeUnder(TreeView, SourceNode.Item, NewNode);
TreeView.Items.Delete(SourceNode);
end;
procedure TForm1.ExecSQL(const S: String);
begin
with dmData.quSQLdo
begin
Close;
SQL.Text := S;
Prepared := True;
ExecSQL;
Prepared := False;
Close;
end;
end;