Solved

Generate a treeview

Posted on 1999-01-11
4
174 Views
Last Modified: 2010-04-06
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?
Please tell me. Thanks.
0
Comment
Question by:mathematics
  • 2
4 Comments
 
LVL 12

Expert Comment

by:rwilson032697
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

by:
apaparis earned 50 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

by:kretzschmar
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 }
  treeview1.Items.Add(treeview1.TopItem,Table1.TableName);
  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
      t := TreeView1.Items.AddChild(TreeView1.Items[i],Table1.FieldByName('Son').AsString);
       { Do Something else with t }
    end
    else
    begin     {Father not exists, add Father to root, add son }
      t := TreeView1.Items.AddChild(TreeView1.TopItem,Table1.FieldByName('Father').AsString);
       { Do Something else with t }
      t := TreeView1.Items.AddChild(t,Table1.FieldByName('Son').AsString);
       { 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

by:apaparis
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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question