數據結構如下: CREATE TABLE [tbTree] ( [ID] [varchar] (4) COLLATE NOT NULL , [Context] [nvarchar] (50) NULL , [ParentID] [varchar] (4) NULL , CONSTRAINT [PK_tbTree] PRIMARY KEY CLUSTERED ( [ID] ) ON [PRIMARY] ) ON [PRIMARY] 代碼如下:
lprocedure TForm1.ReadTree(tnode:TTreeNode;Fvalue: String);
lVar
l i: integer;
l Flist:TStringList;
l Flist1:TStringList;
l str:string;
lbegin
l qryTree.close;
l qryTree.sql.clear;
l qryTree.sql.add('select Parentid,id,context from tbtree');
l qryTree.sql.add('where isnull(Parentid,'''')=');
l qryTree.sql.add(''''+Fvalue+'''');
l qryTree.Open;
l qryTree.First;
l Flist:=TStringList.Create;
l Flist1:=TStringList.Create;
l while not qryTree.eof do
l begin
l Flist.Add(trim(qryTree.fIEldbyname('context').asstring));
l Flist1.Add(qryTree.fIEldbyname('id').asstring);
l qryTree.next;
l end;
l for i:= 0 to flist.Count-1 do
l begin
l s:=flist1.Strings[i];
l str:=flist.Strings[i];
l snode:=TreevIEw1.items.addchild(tnode,str);
l ReadTree(snode,s);
l end;
l flist.free;
l flist1.free;
lend;