数据库,我是用:一个ID,一个ParentID来表示从属关系的。意思你应该知道的吧。
程序,我这里有一个例子,你自己看看吧。
FQry1 := dm.GetQueryComponent;
v_sql := 'Select c.corp_id, c.corp_name, d.depart_id, d.depart_name, t.team_id, t.team_name' + #13#10 +
'From dbo.m_corp_info c Inner Join dbo.m_depart_info d On c.corp_id = d.corp_id Inner Join' + #13#10 +
'dbo.m_team_info t On d.corp_id = t.corp_id And d.depart_id = t.depart_id Order By c.corp_id,' + #13#10 +
'd.depart_id, t.team_id';
try
if dm.QueryDatabase(FQry1, v_sql) then
begin
while not FQry1.Eofdo
begin
if TreeNode1 <> nil then
TreeNode1.Expand(False);
TreeNode1 := TvDoorCard.Items.AddChild(TreeNode0, FQry1.FieldByName('corp_name').AsString);
TreeNode1.ImageIndex := 1;
TreeNode1.SelectedIndex := 1;
New(P);
P^ := FQry1.FieldByName('corp_id').AsString;
TreeNode1.Data := P;
while not FQry1.Eofdo
begin
TreeNode2 := TvDoorCard.Items.AddChild(TreeNode1, FQry1.FieldByName('depart_name').AsString);
TreeNode2.ImageIndex := 1;
TreeNode2.SelectedIndex := 1;
New(P);
P^ := FQry1.FieldByName('depart_id').AsString;
TreeNode2.Data := P;
while not FQry1.Eofdo
begin
TreeNode3 := TvDoorCard.Items.AddChild(TreeNode2, FQry1.FieldByName('team_name').AsString);
TreeNode3.ImageIndex := 2;
TreeNode3.SelectedIndex := 2;
New(P);
P^ := FQry1.FieldByName('team_id').AsString;
TreeNode3.Data := P;
FQry1.Next;
if string(TreeNode2.Data^) <> FQry1.FieldByName('depart_id').AsString then
Break;
end;
FQry1.Next;
if string(TreeNode1.Data^) <> FQry1.FieldByName('corp_id').AsString then
Break;
end;
FQry1.Next;
end;
end;
finally
FQry1.Close;
FQry1.Free;
TvDoorCard.Items.EndUpdate;
TreeNode0.Expand(False);
for i := 0 to TreeNode0.Count - 1do
TreeNode0.Item.Expand(False);
end;