我的一段程序,你参考一下!
procedure TFrmOperator.InitTreeView(TreeView1:TTreeView;Userid:string;has:Boolean);
var
AdoTemp,Adotemp1,adotemp2:TadoDataset;
TreeNode1,TreeNode2,TreeNode3:TTreeNode;
WhereSql,sql:string;
begin
TreeView1.Items.Clear;
if has then
WhereSql:=' and id in (select modulid from tblpower where No='''+userid+''')'
else
WhereSql:=' and id not in (select modulid from tblpower where No='''+userid+''')';
if Userid='All' then
WhereSql:='';
AdoTemp:=TadoDataset.Create(nil);
AdoTemp.Connection:=AdoConnection1;
AdoTemp.CommandText:='select * from Tblmodul where upid=''0''';
AdoTemp.Open;
if AdoTemp.IsEmpty then
begin
AdoTemp.Free;
exit;
end;
adotemp1:=Tadodataset.Create(nil);
adotemp1.Connection:=adoconnection1;
adotemp2:=Tadodataset.Create(nil);
adotemp2.Connection:=adoconnection1;
AdoTemp.First;
while not AdoTemp.Eof do
begin
new(TreeData);
TreeData^.id:=AdoTemp.fieldbyname('id').asstring;
TreeData^.name:=AdoTemp.fieldbyname('name').AsString;
TreeData^.Subitem:=AdoTemp.fieldbyname('Subitem').AsBoolean;
TreeData^.UpId:=AdoTemp.fieldbyname('Upid').AsString;
adotemp1.Close;
adotemp1.CommandText:='select * from tblmodul where upid='''+TreeData^.id+''''+WhereSql;
adotemp1.Open;
if adotemp1.IsEmpty then
begin
if TreeData^.Subitem =False then
begin
TreeNode1:=TreeView1.Items.Add(nil,TreeData^.name);
TreeNode1.Data :=TreeData;
end;
AdoTemp.Next;
continue;
end;
TreeNode1:=TreeView1.Items.Add(nil,TreeData^.name);
TreeNode1.Data :=TreeData;
adotemp1.First;
while not adotemp1.Eof do
begin
new(TreeData);
TreeData^.id :=adotemp1.fieldbyname('id').AsString;
TreeData^.name:=adotemp1.fieldbyname('name').AsString;
TreeData^.Subitem:=AdoTemp1.fieldbyname('Subitem').AsBoolean ;
TreeData^.UpId:=AdoTemp1.fieldbyname('Upid').AsString;
adotemp2.Close;
sql:='select * from tblmodul where upid='''+TreeData^.id+''''+WhereSql;
adotemp2.CommandText:=sql;
adotemp2.Open;
if adotemp2.IsEmpty then
begin
if TreeData^.Subitem =False then
begin
TreeNode2:=TreeView1.Items.AddChild(TreeNode1,TreeData^.name);
TreeNode2.Data :=TreeData;
end;
adotemp1.Next;
continue;
end;
TreeNode2:=TreeView1.Items.AddChild(TreeNode1,TreeData^.name);
TreeNode2.Data :=TreeData;
adotemp2.First;
while not adotemp2.Eof do
begin
new(TreeData);
TreeData^.id :=adotemp2.fieldbyname('id').AsString;
TreeData^.name:=adotemp2.fieldbyname('name').AsString;
TreeData^.Subitem:=AdoTemp2.fieldbyname('Subitem').AsBoolean ;
TreeData^.UpId:=AdoTemp2.fieldbyname('Upid').AsString;
TreeNode3:=TreeView1.Items.AddChild(TreeNode2,TreeData^.name);
TreeNode3.Data :=TreeData;
adotemp2.Next;
end;
adotemp1.Next;
end;
AdoTemp.Next;
end;
adotemp.free;
adotemp1.Free;
adotemp2.Free;
end;