1.选取增加的节点
var
Nodeo: TTreeNode;
begin
Nodeo := TreeView1.Selected;
TreeView1.Items.AddChild(Nodeo, '子节点 ');
ADOQuery1.Edit;
DBGrid.Fields[1].AsString := '子节点 ';
ADOQuery1.Post;
...
end;
2.
给你整个递归的 (一)
procedure TFrmMain.Button1Click(Sender: TObject);
var
GNode:TTreeNode;
begin
with dm.adoQuery do
begin
close;
sql.clear;
Sql.Add('Select * from _Test where DepP='''+'0'+'''');
open;
while not eof do
begin
Gnode := treeview1.Items.Add(nil,Fieldbyname('DepN').AsString);
CreateTree(Fieldbyname('DepId').AsString,Gnode);
next;
end;
end;
end;
//parentcode:父节点编码
procedure TFrmMain.CreateTree(parentcode:string;cntNode:TTreeNode);
var
Node:TTreeNode;
qry:TADOQuery;
begin
qry := TADOQuery.Create(nil);
qry.Connection := dm.AdoCon;
with qry do
begin
close;
sql.clear;
Sql.Add('Select * from _Test where DepP='''+parentcode+'''');
open;
while not eof do
begin
Node:=treeview1.Items.AddChild(cntNode,Fieldbyname('DepN').AsString);
CreateTree(Fieldbyname('DepId').AsString, Node);
next;
end;
end;
qry.Free;
end;
递归(二)
///////////////////////////////////////////////////////////
//
// 增加Treeview.
//
///////////////////////////////////////////////////////////
procedure Tfrmmain.AddClass(AId:integer;FatherNode:TTreeNode);
var
QryTmp:TADOQuery;
myNode:TTreeNode;
myLabel:TLabel;
begin
QryTmp := TADOQuery.Create(self);
QryTmp.Connection := ADOConnection1;//.ConnectionString:= 'Provider=Microsoft.Jet.OLEDB.4.0;Data Source= '+extractfilepath(application.ExeName)+ 'test.mdb;Persist Security Info=False ';
QryTmp.SQL.Add( 'select * from tb1 ');
QryTmp.SQL.Add( 'where FatherId= '+inttostr(AId));
QryTmp.Open;
while not QryTmp.Eof do
begin
myNode:=Treeview1.Items.AddChild(FatherNode,QryTmp.fieldbyname( 'CName ').AsString);
//创建标签,caption存放各分支的AutoId表识
myLabel:=TLabel.Create(self);
myLabel.Visible:=false;
myLabel.Caption:=QryTmp.fieldbyname( 'AutoId ').AsString;
myNode.Data:=myLabel;
myNode.ImageIndex := QryTmp.FieldByName( 'IconIndex ').AsInteger;
AddClass(QryTmp.fieldbyname( 'AutoId ').AsInteger,myNode); //递归调用过程
QryTmp.Next;
end;
QryTmp.Free;
end;