unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ComCtrls, DB, ADODB, ImgList;
type
Tfrm = class(TForm)
btn: TButton;
qry1: TADOQuery;
imaList: TImageList;
tvw1: TTreeView;
lbl1: TLabel;
lbl2: TLabel;
lbl3: TLabel;
edtID: TEdit;
edtName: TEdit;
edtBelong: TEdit;
procedure FormCreate(Sender: TObject);
procedure tvw1Change(Sender: TObject;
Node: TTreeNode);
procedure btnClick(Sender: TObject);
private
procedure FmtTree;
public
{ Public declarations }
end;
var
frm: Tfrm;
implementation
{$R *.dfm}
procedure Tfrm.FmtTree();
var
i,j :integer;
leafList,leafListPlus: TList;
leaf,subNode: TTreeNode;
tv: TTreeView;//引用窗体控件
qry: TADOQuery;//引用窗体控件
begin
//初始化
leafList:=TList.Create;
leafListPlus:=TList.Create;
tv:=tvw1;
tv.Items.Clear;
qry:=qry1;
subNode:=tv.Items.AddChild(nil,'月夜风筝(我)的公司');
leafList.Add(subNode);
//处理
while leafList.Count > 0do
begin
leafListPlus.Clear;
for i:=0 to leafList.Count-1do
begin
leaf:=leafList;
if leaf.Level = 0 then
qry.SQL.Text:=Format('select code,name,belong from TB where belong = ''%s''',['--'])
else
qry.SQL.Text:=Format('select code,Name,belong from TB where belong = ''%d''',[leaf.StateIndex]);
qry.Open;
for j:= 0 to qry.RecordCount-1do
begin
subNode:=tv.Items.AddChild(leaf,qry.FieldByName('name').AsString);
subNode.ImageIndex:=subNode.Level;
subNode.StateIndex:=StrToInt(qry.FieldByName('code').AsString);
leafListPlus.Add(subNode);
qry.Next;
end;
end;
leafList.Assign(leafListPlus);
end;
//清理
tv.FullExpand;
leafListPlus.Free;
leafList.Free;
end;
procedure Tfrm.btnClick(Sender: TObject);
begin
FmtTree;
end;
procedure Tfrm.tvw1Change(Sender: TObject;
Node: TTreeNode);
begin
edtID.Text:=InttoStr(tvw1.Selected.StateIndex);
edtName.Text:=tvw1.Selected.Text;
if tvw1.Selected.Level <> 0 then
edtBelong.Text:=tvw1.Selected.Parent.Text
else
edtBelong.Text:='无';
end;
procedure Tfrm.FormCreate(Sender: TObject);
begin
qry1.Close;
qry1.ConnectionString:='Provider=Microsoft.Jet.OLEDB.4.0;Data Source='+ExtractFilePath(ParamStr(0))+'DBTree.mdb;Persist Security Info=False';
end;
end.