Delphi动态创建树
2006-02-04 13:51:25 来源:WEB开发网核心提示:数据结构如下: CREATE TABLE [tbTree] ( [ID] [varchar] (4) COLLATE NOT NULL , [Context] [nvarchar] (50) NULL , [ParentID] [varchar] (4) NULL , CONSTRAINT [PK_tbTree]
数据结构如下:
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;
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;
赞助商链接