表结构及记录
object tarea: TFDQuery
Connection = FDConnection1
SQL.Strings = (
'select *'
'from tArea order by fParentCode')
Left = 160
Top = 240
end
object tv: TTreeView
Left = 0
Top = 0
Width = 554
Height = 320
Align = alClient
Indent = 19
TabOrder = 1
ExplicitLeft = 40
ExplicitTop = 32
ExplicitWidth = 121
ExplicitHeight = 97
end
代码
procedure TForm1.FormCreate(Sender: TObject);
var
nodes:ttreenodes;
node:ttreenode ;
CurItem ,CurItem1:Ttreenode ;
nodestr:string;
lastparent,temp:string;
i,j:integer ;
begin
TV.Items.Clear;
nodes:=TV.Items;
Tarea.Active:=true;
node:=nodes.add(nil,Tarea.FieldByName('fCode').Value+'_'+Tarea.FieldByName('fNAME').Value);
Tarea.Next;
while NOT Tarea.Eof do
BEGIN
if Tarea.FieldByName('fParentCode').AsString='' then
nodes.add(nil,Tarea.FieldByName('fCode').Value+'_'+Tarea.FieldByName('fNAME').Value); //省份
if Tarea.FieldByName('fParentCode').AsString<>lastparent then
//当前记录 fParentCode跟上条记录 fParentCode 不相等,表明是换一个父代了
//需查找
begin
CurItem := TV.Items.GetFirstNode;
while CurItem <> nil do
begin
if copy( CurItem.Text,1,pos('_',CurItem.Text)-1) =Tarea.FieldByName('fParentCode').Value then
begin
CurItem.Selected:=true;
break;
end;
CurItem := CurItem.GetNext;
end;
nodes.addCHILD(CurItem,Tarea.FieldByName('fCode').Value+'_'+Tarea.FieldByName('fNAME').Value); //
end;
if (Tarea.FieldByName('fParentCode').AsString<>'')
AND
(Tarea.FieldByName('fParentCode').AsString=lastparent)
//当前记录 fParentCode跟上条记录 fParentCode 相等,表明是同一个父代了,直接添加就行
then
BEGIN
nodes.addCHILD(CurItem,Tarea.FieldByName('fCode').Value+'_'+Tarea.FieldByName('fNAME').Value); //
END;
lastparent:= Tarea.FieldByName('fParentCode').AsString;
Tarea.Next;
END;
nodes.GetFirstNode;
end;
end.
效果如下
|
请发表评论