有个数据库结构id         fatherid         text
2              0             总类
3              2             军事
4              2             文化
5              2             教育 
6              3             空军  
7              3             陆军
8              5             大学
9              8             北京大学
现想用递归的方法把 TEXT的字段内容分层次的在TTreeView里显示出来如何做?
       

解决方案 »

  1.   

    用TDBTreeView就可以了,分别设置它的ParentID, ChildID属性
      

  2.   

    要是自己写好像满麻烦的,现成的同意 AWolfBoy(龍行江湖)  
      

  3.   

    TDBTreeView在DELPHI的那个栏目下?
      

  4.   

    http://www.cx66.com/cxgzs/program/delphi/295.htm
    派托你去google搜一下,一堆呢
      

  5.   

    addobject
    for instance:type
    PMyRec = ^TMyRec;
    TMyRec = record
      FName: string;
      LName: string;
    end;Assuming these types are used, the following code adds a node to TreeView1 as the last sibling of a specified node. A TMyRec record is associated with the added item. The FName and LName fields are obtained from edit boxes Edit1 and Edit2. The Index parameter is obtained from edit box Edit3. The item is added only if the Index is a valid value.procedure TForm1.Button1Click(Sender: TObject);var
      MyRecPtr: PMyRec;
      TreeViewIndex: LongInt;
    begin
      New(MyRecPtr);
      MyRecPtr^.FName := Edit1.Text;
      MyRecPtr^.LName := Edit2.Text;
      TreeViewIndex := StrToInt(Edit3.Text);
      with TreeView1 do
      begin
        if Items.Count = 0 then
          Items.AddObject(nil, 'Item' + IntToStr(TreeViewIndex), MyRecPtr)
        elseif (TreeViewIndex < Items.Count) and (TreeViewIndex >= 0) then
          Items.AddObject(Items[TreeViewIndex], 'Item' + IntToStr(TreeViewIndex), MyRecPtr);  end;
    end;After an item containing a TMyRec record has been added, the following code retrieves the FName and LName values associated with the item and displays the values in a label.procedure TForm1.Button2Click(Sender: TObject);begin
      Label1.Caption := PMyRec(TreeView1.Selected.Data)^.FName + ' ' +
                      PMyRec(TreeView1.Selected.Data)^.LName;
    end;
    ////////////////////////////////////////////////////////////////////////
    AddChildObject:
    TreeView1.Items.AddChildObject(TreeView1.Selected,'New Item',MyBitMap); 
    //--------------------------------------------------------------------
    addprocedure TForm1.Button1Click(Sender: TObject);var
      MyTreeNode1, MyTreeNode2: TTreeNode;
    begin
      with TreeView1.Items do
      begin
        Clear; { remove any existing nodes }
        MyTreeNode1 := Add(nil, 'RootTreeNode1'); { Add a root node }
        { Add a child node to the node just added }
        AddChild(MyTreeNode1,'ChildNode1');    {Add another root node}
        MyTreeNode2 := Add(MyTreeNode1, 'RootTreeNode2');
        {Give MyTreeNode2 to a child }
        AddChild(MyTreeNode2,'ChildNode2');    {Change MyTreeNode2 to ChildNode2 }
        { and add a child node to it}
        MyTreeNode2 := TreeView1.Items[3];
        AddChild(MyTreeNode2,'ChildNode2a');    {Add another child to ChildNode2, after ChildNode2a }
        Add(MyTreeNode2,'ChildNode2b');    {add another root node}
        Add(MyTreeNode1, 'RootTreeNode3');
      end;end;