typeorm
PDepData=^DepData;get
DepData = recordstring
ID: Integer;it
DepID: string;io
DepName: string;function
end;di
procedure FreeTV(tv: TTreeview);view
varvi
i: Integer;while
begin
for i := 0 to tv.Items.Count - 1 do
begin
dispose(tv.Items[i].Data);
end;
tv.Items.Clear;
end;
procedure LoadTree(tv: TTreeview; dst: TDataset);
function GetNodeData: PDepData;
begin
new(result);
with result^ do begin
ID := dst.FieldByName('ID').AsInteger;
DepID := dst.FieldByName('DepID').AsString;
DepName := dst.FieldByName('DepName').AsString;
end;
end;
var
ParentNode, NewNode: TTreeNode;
p: PDepData;
found: Boolean;
i: Integer;
begin
FreeTV(tv); // 釋放樹視圖
dst.First;
ParentNode := nil;
while not dst.Eof do begin
p := GetNodeData;
found := false;
for i := tv.Items.Count-1 downto 0 do begin // 找 雙親節點
if pos(PDepData(tv.Items[i].Data).DepID, p.DepID) = 1 then begin
ParentNode := tv.Items[i];
found := true;
break;
end;
end;
if not found then begin // 未找到雙親節點,加到樹根下
ParentNode := tv.Items.Add(nil, p.DepName);
ParentNode.Data := p;
end else // 找到雙親節點,加到雙親節點下
tv.Items.AddChild(ParentNode, p.DepName).Data := p;
dst.Next;
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
t: Cardinal;
begin
t := gettickcount;
LoadTree(tv, MyQuery1);
t := gettickcount - t;
caption := inttostr(t);
end;