这是有父级编码的树,很简单的
给你个例子
//按上级编码建树 fbjbm为本级编码,fsjbm为上级编码,fbjmc为本级名称,fexbm为扩展内部编码
procedure CreateTree(PQuery:TDataSet;fbjbm,fsjbm,fbjmc:string;Separator:string);
//-----------------------------------------------------------------------------
procedure TTreeFrame.CreateTree(PQuery: TDataSet; fbjbm, fsjbm,
fbjmc: string;Separator:string);
var
code,bh:string;
FNode
char;
i:integer;
str:string;
lTreeNode,lTreeNode1:TTreeNode;
begin
Clear;
//*******************************************
if not pquery.Active then exit;
//创建第一层
str:=fsjbm +'= ''00''';
pquery.Filter:=str;
pquery.Filtered:=true;
pquery.First;
while not pquery.Eof do
begin
bh:=pquery.fieldbyname(fbjbm).asstring;
code:=pquery.fieldbyname(fbjmc).asstring;
try
getmem(FNode,length(bh)+1);//得到指针的内存空间
except
exit;
end;
if Separator <> '' then code:=bh + Separator + code;
if length(code) >255 then code :=copy(code,0,255);
move(bh[1],FNode^,length(bh)+1);//指针指出结点对应的编码
treeview.Items.AddObject(nil,code, FNode);
pquery.Next;
Application.ProcessMessages;
end;
lTreeNode:=TreeView.Items.GetFirstNode;
while lTreeNode<>nil do
begin
FNode:=lTreeNode.Data;
str:=fsjbm+' = '+#39+FNode+#39;
pquery.Filter:='';
pquery.Filtered:=false;
pquery.Filter:=str;
pquery.Filtered:=true;
pquery.First;
while not pquery.Eof do
begin
bh:=pquery.fieldbyname(fbjbm).asstring;
code:=pquery.fieldbyname(fbjmc).asstring;
try
getmem(FNode,length(bh)+1);//得到指针的内存空间
except
exit;
end;
if Separator <> '' then code:=bh + Separator + code;
if length(code) >255 then code :=copy(code,0,255);
move(bh[1],FNode^,length(bh)+1);//指针指出结点对应的编码
treeview.Items.AddChildObject(lTreeNode, code, FNode);
pquery.Next;
Application.ProcessMessages;
end;//while do
lTreeNode:=lTreeNode.GetNext;
end;//while do
end;
如果数据量比较大,建议用分层建树
如有问题就联系我