你这种结构不好写,加一个字段ParentID(即中类的父节点为大类ID,小类的父节点为中类ID),再用递归函数:
procedure TForm_Postion.InitTv(ParentNode: TTntTreeNode; ParentID: Integer);
Var
ADOInit:TADOQuery;
sqlstr:widestring;
PTv:Pinteger;
ChildNode:TTntTreeNode;
b_Last:boolean;
begin
ADOInit:=TADOquery.Create(self);
ADOInit.Connection:=DM.ADOCon;
TV.Items.BeginUpdate;
Try
if MainDisPostion.Checked then
sqlstr:='select * from Sys_Position where FiParentID='+IntToStr(ParentID)
else
sqlstr:='select * from Sys_Position where FbStop=0 and FiParentID='+IntToStr(ParentID);
with ADOInit do
begin
Close;
sql.Clear;
sql.Add(sqlstr);
Open;
end;
if ADOInit.RecordCount<>0 then
begin
while Not ADOInit.Eof do
begin
New(PTv);
PTv^:=ADOInit.Fieldbyname('ID').AsInteger;
ChildNode:=Tv.Items.AddChildObject(ParentNode,TTntWideStringField(ADOInit.Fieldbyname('FvName')).AsWideString,PTv);
if Not ADOInit.FieldByName('FbIsLast').AsBoolean then
begin
b_Last:=false;
ChildNode.ImageIndex:=0;
ChildNode.SelectedIndex:=0;
end
else
begin
ChildNode.ImageIndex:=1;
ChildNode.SelectedIndex:=1;
b_Last:=True;
end;
if Not b_Last then
InitTv(ChildNode,PTV^);
ParentNode.Expanded:=True;
ChildNode.Expanded:=True;
ADOInit.Next;
end;
end;
Finally
ADOInit.Free;
end;
Tv.Items.EndUpdate;
end;
调用:
Var
TreeNode:TTntTreeNode;
PT:Pinteger;
begin
Tv.Items.Clear;
New(PT);
PT^:=0;
TreeNode:=Tv.Items.AddChildObject(nil,顶级节点,PT);
InitTv(TreeNode,0);
end;