Board index » delphi » Using TTreeView with DB for parent/child relationship

Using TTreeView with DB for parent/child relationship

Hi,

I have a database with the following structure:

DB structure:
1. Rec no.
2. Parent
3. Name

How can I use the treeview component to display the relationship of parent
child from the database?  Can someone provide me with code to do this?

For example: ( DB contents )

Rec. no.    Parent       Name
1              -1              Name1
2              1               Name 2
3.             1               Name 3
4.             2               Name 4
5.             3               Name 5

Should display

            Name 1
                 |
                +------Name 2
                 |             |
                 |            +------- Name 4
                 |
                +------Name 3
                              |
                             +--------Name 5

Thanks.
Bob.

 

Re:Using TTreeView with DB for parent/child relationship


Ok, here goes:

procedure TForm1.Button1Click(Sender: TObject);

  function GetNode(RecNo: Integer): TTreeNode;
  var
    i: Integer;
  begin
    for i := 0 to TreeView1.Items.Count - 1 do
      if Integer(TreeView1.Items[i].Data) = RecNo then
      begin
        Result := TreeView1.Items[i];
        Exit;
      end;
    Result := nil;
  end;

var
  Node, ParentNode: TTreeNode;
begin
  while Table1.RecordCount > TreeView1.Items.Count do
  begin
    Table1.First;
    while not Table1.EOF do
    begin
      Node := GetNode(Table1.FieldByName('RecNo').AsInteger);
      if not Assigned(Node) then
      begin
        if Table1.FieldByName('Parent').AsInteger >= 0 then
          ParentNode := GetNode(Table1.FieldByName('Parent').AsInteger)
        else
          ParentNode := nil;
        if Assigned(ParentNode) then
          Node := TreeView1.Items.AddChild(ParentNode,
            Table1.FieldByName('Name').AsString)
        else
          Node := TreeView1.Items.Add(nil,
            Table1.FieldByName('Name').AsString);
        Node.Data := Pointer(Table1.FieldByName('RecNo').AsInteger);
      end;
      Table1.Next;
    end;
  end;
end;

"Robert Okemo" <rok...@ecvision.com> schreef in bericht
news:8gvtm0$t2q$1@hfc.pacific.net.hk...

Quote
> Hi,

> I have a database with the following structure:

> DB structure:
> 1. Rec no.
> 2. Parent
> 3. Name

> How can I use the treeview component to display the relationship of parent
> child from the database?  Can someone provide me with code to do this?

> For example: ( DB contents )

> Rec. no.    Parent       Name
> 1              -1              Name1
> 2              1               Name 2
> 3.             1               Name 3
> 4.             2               Name 4
> 5.             3               Name 5

> Should display

>             Name 1
>                  |
>                 +------Name 2
>                  |             |
>                  |            +------- Name 4
>                  |
>                 +------Name 3
>                               |
>                              +--------Name 5

> Thanks.
> Bob.

Other Threads