Solved

Generate a treeview

Posted on 1999-01-11
4
176 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

749 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