Solved

How to construct a treeview from a relational database

Posted on 1999-01-22
8
176 Views
Last Modified: 2010-04-06
A table with fields son,father,data.
son is the son of the father in one tree.
now,please tell me any method to get the treeview from the
table.,
Thank you.
0
Comment
Question by:mathematics
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 27

Expert Comment

by:kretzschmar
Comment Utility
Hi Mathematics,

See my comment, at your q:

http://www.experts-exchange.com/topics/comp/lang/delphi/Q.10116479

meikl
0
 
LVL 1

Accepted Solution

by:
wamoz earned 50 total points
Comment Utility
What you are describing is a classical problem known as the "Explosion of Parts" problem. It is intractable in a declarative language that does not support recursive evaluation, and unfortunately SQL falls into this category.

On the other hand the problem yields easily to iterative treatment.

What you should do is write a parametric SQL statement that selects all the offspring of a nominated individual, and run this each time a childless node is expanded. In the event handler procedure for node expansion you need to obtain the UID of the entity that the node represents, and use this as the parameter governing the list of entities that your query will return. You use this result set to add children to the node that is expanding.

This approach can even deal with circular references.
0
 
LVL 27

Expert Comment

by:kretzschmar
Comment Utility
hi mathematics,

I've pasted my comment here

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, i.e. some data in the node.data option }
    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  i.e. some data in the node.data option}
      t := TreeView1.Items.AddChild(t,Table1.FieldByName('Son').AsString);
       { Do Something else with t  i.e. some data in the node.data option}
    end;
    Table1.Next; {Next Record}
  end;
  table1.Close;
end;


meikl
0
 

Author Comment

by:mathematics
Comment Utility
Thanks everybody.
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 27

Expert Comment

by:kretzschmar
Comment Utility
Hi mathematics,

i wonder that the theoretical answer from wamoz helped you?

meikl

0
 

Author Comment

by:mathematics
Comment Utility
Thank kretzschmar!
Your comment is useful for me before.
I am glad to every answer.
Hope you understand.
0
 

Expert Comment

by:MBerlin
Comment Utility
wamoz theoretical answer was useful to me too.
I've come across a similar problem and solved it as wamoz mentioned but I wondered if SQL had a better way that I did not know.
wamoz says no, sql does not have a better way.
thanks for putting that doubt to rest
0
 
LVL 1

Expert Comment

by:wamoz
Comment Utility
The failure of SQL to support recursive evaluation is by design (ANSI committee). Relations containing circular references CANNOT be automatically materialised because they contain an infinite number of tuples.

I have written custom SQL interpreters that deal with this problem by terminating branch evaluation on detection of repeated tuples. However, when presenting this kind of information in a tree widget, it is actually *easier* to fetch one "child set" at a time as previously described, and as this leads to optimal performance, this is the preferred approach.

As a famously anonymous person once said, "If it aint broke, don't fix it."
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

763 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

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now