Treeview with DB problem D7/ADO


2005-05-07 11:49:18 AM
delphi208
Hi all!
Not sure if this is the right forum for this question
I'm busy creating a family tree app and has run into a problem. After retrieving parent and child information from database, I tried to add them in a treeview by looping through the database results. To explain further, my tables have only two parent info in them, with one set of parents having more than one child, but when i add the results to the treeview, each parent name is retrieved and displayed(with children) according to the number of children they have. For example:
if you have parent1 with 2 children AND parent2 with 3 children
then name of parent1 will be displayed twice, with one of the children's names each and likewise with parent2. Whilst the correct way it should display this info is: parent1 with two children and parent2 with 3 children.
Here's the code i use to retrieve and display the info:
procedure TForm1.Button1Click(Sender: TObject);
begin
q1.Close;
q1.SQL.Text:='select [person.name],[child.name] from [child],[person] where [child.pid]=[person.pid]';
q1.Open;
while not q1.EOF do
begin
with tv_eg1.Items.AddFirst(nil, 'Parents - ' + q1.fieldbyname('person.name').Text ) do
begin
{Select the root node}
Selected := true;
tv_eg1.Items.AddChild( tv_eg1.Selected, 'Child - ' + q1.fieldbyname('child.name').Text );
q1.Next;
end;
end;
end;
I hope this makes sense!