//下面是以往寫的顯示多層資產結構,
//可以控制任意層,並且可以在樹結點中邦定有關數據
TAsset_type=class
public
Type_id:string;
parent:String;
Type_no:String;
Type_name:String;
Types:String;
procedure setData(id,p,no,name,typs:string);
end;
---------
procedure LoadAssetType;
var
curNode:TtreeNode;
tmpNode:TtreeNode;
nodeData:TAsset_type;
Type_id:String;
Tado:Tadoquery;
begin
Treeview1.Items.Clear;
Astack:=TStack.Create;//建棧
curNode:=TreeView1.Items.Add(TreeView1.TopItem,FirstNodeTxt);
curNode.ImageIndex:=1;
curNode.Expanded:=true;
nodeData:=TAsset_type.Create;//建立treeview結點數據結構
nodeData.setData('00000','','',FirstNodeTxt,'A');//設定你的第一個結點
curNode.Data:=nodeData;//邦定結構
Astack.Push(curNode);//進棧
while Astack.Count>0 do
begin
curNode:=TTreeNode(Astack.Pop);//取出結構
Type_id:=TAsset_type(curNode.Data).Type_id;//取出父結點
//以下是查找出與父結點相同的結點來的.用你的重寫
Tado:=MainDataModule.GetData('select * from wsAsset_type where parent='+''''+type_id+''''+' order by type_id');
while not Tado.Eof do
begin
nodeData:=TAsset_type.Create;
nodeData.setData(Tado.fieldbyname('Type_id').asstring,
Tado.fieldbyname('parent').asstring,
Tado.fieldbyname('Type_no').asstring,
Tado.fieldbyname('Type_name').asstring,
Tado.fieldbyname('Types').asstring);
tmpNode:=TreeView1.Items.AddChild(curNode,nodeData.Type_no+' '+nodeData.Type_name);//建立樹結點
tmpNode.ImageIndex:=0;
tmpNode.Data:=nodeData;//邦定
Astack.Push(tmpNode);//重新入棧
Tado.Next;
end;
Tado.Close;
end;
Treeview1.TopItem.Expand(false);
end;