不需要递归,一次只读取一层数据,下一层用临时结点代替,展开时才真正读取数据,<br>这样可以大大加快速度,因为一次只读一层数据,而不是读取整个树的数据.<br><br>代码如下:<br><br>//创建根结点<br>procedure TForm1.FormCreate(Sender: TObject);<br>var<br> Node: TTreeNode;<br> Text: string;<br>begin<br> TreeView1.Items.Clear;<br> with ADOQuery1 do<br> begin<br> Close;<br> SQL.Text:= 'select distinct 组号 from 表1 where 组号 not in (select 成员编号 from 表1)';<br> Open;<br> First;<br> while not Eof do<br> begin<br> Text:= FieldByName('组号').AsString;<br> Node:= TreeView1.Items.Add(nil, Text);<br> TreeView1.Items.AddChild(Node, ''); //此为临时结点,<br> Next;<br> end;<br> end;<br>end;<br><br>//展开时删除临时结点,并创建真正的结点<br>procedure TForm1.TreeView1Expanding(Sender: TObject; Node: TTreeNode;<br> var AllowExpansion: Boolean);<br>var<br> ChildNode: TTreeNode;<br> Text: string;<br>begin<br> if (Node.Count=1) and (Node.getFirstChild.Text='') then<br> begin<br> Node.DeleteChildren; //删除临时结点<br> with ADOQuery1 do<br> begin<br> Close;<br> SQL.Text:= 'select distinct 成员编号 from 表1 where 组号=''' + Node.Text + '''';<br> Open;<br> First;<br> while not Eof do<br> begin<br> Text:= FieldByName('成员编号').AsString;<br> ChildNode:= TreeView1.Items.AddChild(Node, Text);<br> TreeView1.Items.AddChild(ChildNode,''); //此为临时结点,<br> Next;<br> end;<br> end;<br> end;<br>end;