procedure TForm1.FormCreate(Sender: TObject);
var
I,Y,L:Integer;
Bstr,Sustr:string;
RootNode,SubNode,SutNode:TTreeNode;
begin
try
ADOConnection1.ConnectionString:='Provider=Microsoft.Jet.OLEDB.4.0;Password="";Data Source='+ GetCurrentDir + '\db\mydb.mdb;Persist Security Info=True';
ADOConnection1.LoginPrompt:=False;
ADOConnection1.Connected:=True;
ADOQuery1.Connection:=ADOConnection1;
ADOQuery2.Connection:=ADOConnection1;
ADOQuery3.Connection:=ADOConnection1;
except
Application.MessageBox('数据库连接错误','错误',16);
Application.Terminate;
end;
with ADOQuery1 do
begin
Close;
SQL.Clear;
SQL.Add('select distinct Rep_name from Rep_all');
Open;
First;
for I := 0 to ADOQuery1.RecordCount - 1 do     //根节点循环
begin
Bstr:=ADOQuery1.FieldByName('Rep_name').AsString;   //获得根节点名称
RootNode:=RzCheckTree1.Items.Add(nil,Bstr);        //创建根节点
with ADOQuery2 do
begin
Close;
SQL.Clear;
SQL.Add('select * from Rep_all where Rep_name='''+ Bstr +'''');   //通过根节点查询子节点
Open;
First;
for Y := 0 to ADOQuery2.RecordCount - 1 do    //子节点循环
begin
Sustr:=FieldByName('Rep_subname').AsString;
SubNode:=RzCheckTree1.Items.AddChild(RootNode,FieldByName('Rep_subname').AsString);    //创建子节点
with ADOQuery3 do
begin
Close;
SQL.Clear;
SQL.Add('select * from Rep_opt where opt_name=''' + Sustr +'''');
Open;
First;
for L := 0 to ADOQuery3.RecordCount - 1 do      //第2个子节点
begin
SutNode:=RzCheckTree1.Items.AddChild(SubNode,FieldByName('Rep_opt').AsString);
Next;  //第2个子节点
end;
end;
Next;   //子节点
end;
end;
Next;   //根节点
end;
end;
end;

 

二、获取用户选择的值

1.uses commctrl;
2.按钮代码
procedure TForm1.quzhiClick(Sender: TObject);
var
Qnode:TTreeNode;
Qtvi:TTVItem;
begin
Memo1.Lines.Clear; //先清除Memo1中的数据
for Qnode in RzCheckTree1.Items do
begin
Qtvi.mask:= TVIF_STATE; //or code 8
Qtvi.hItem:=Qnode.ItemId;
TreeView_GetItem(RzCheckTree1.Handle,Qtvi);
if Qtvi.state and $2000=$2000 then
begin
Memo1.Lines.Add(Qnode.Text);  //获得用户选择值写入到Memo1中
end;
end;
end;