x
Solved

# Generate a treeview

Posted on 1999-01-11
Medium Priority
182 Views
I have a dataset with fields 'son and 'father'.
(root with father=spaces)(likely subdirectory directory)
Do you know any algorithm generate a tree with
this dataset?
0
Question by:mathematics
• 2

LVL 12

Expert Comment

ID: 1355996
There are some data aware tree views around. Take a look on DSP(http://sunsite.icm.edu.pl/delphi) - there is a search engine.

Cheers,

Raymond.
0

LVL 1

Accepted Solution

apaparis earned 200 total points
ID: 1355997
If Id is primary key
and father is pointing to father record

// Body
DataSet = "Select * from table where father = 0 order by Id"
while not eof begin
appendSubtree(Id)
DataSet.Next
end

Procedure appendSubtree(AnId)
currentTreeNode := <-- append current rec to tree root
SubDataSet := "Select * from Table Where Father = " + IntToStr(AnId)

While Not SubDataSet.Eof Begin
AppendSubTree (Id)
SubDataSet.Next
End
End

0

LVL 27

Expert Comment

ID: 1355998
hi mathematics,

instead teh table i used, you can do it with a dataset also,

procedure TForm1.Button1Click(Sender: TObject);
var
Found : Boolean;
I : Integer;
t : TTreeNode;
begin
{ Clear Treeview }
while treeview1.Items.count > 0 do treeview1.Items[0].delete;
{ Create a root }
table1.open;
table1.first;
while not(Table1.eof) do
begin
i := 0;
Found := False;

{ Search for Father }

while Not(Found) and (i < TreeView1.Items.Count) do
begin
Found := Table1.FieldByName('Father').AsString = Treeview1.Items[i].Text;
if Not(Found) then inc(i);
end;

If Found then  {Father exists, add son }
begin
{ Do Something else with t }
end
else
{ Do Something else with t }
{ Do Something else with t }
end;
Table1.Next; {Next Record}
end;
table1.Close;
end;

can be optimized, of course

meikl
0

LVL 1

Expert Comment

ID: 1355999
Sorry. This is right

If Id is primary key
and father is pointing to father record

// Body
DataSet = "Select * from table where father = 0 order by Id"
while not eof begin
currentNode := <-- append current rec to tree root
appendSubtree(Id, CurrentNode)
DataSet.Next
end

Procedure appendSubtree(AnId, ANode)
SubDataSet := "Select * from Table Where Father = " + IntToStr(AnId)

While Not SubDataSet.Eof Begin
SubTreeNode := <-- append current rec under ANode
AppendSubTree (Id, SubTreeNode)
SubDataSet.Next
End
End

0

## Featured Post

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.