求救!!!建多级树的原代码,高分相送!小第在次先谢谢各位大侠了!

解决方案 »

  1.   

    http://expert.csdn.net/Expert/topic/2281/2281015.xml?temp=.9590418
    看最后,我写了答案的
      

  2.   

    我想建一棵从数据库中取数据的,数据库中有一张表department
    表中字段是这样的!
        department                   name NOT NULL,                /*部门号*/
        department_name              full_name NOT NULL,           /*部门名称 */
        parent_id                    name NOT NULL,                /*上级部门名称 */
        description                  information not null,         /*部门描述*/ 
        principal_staff              staff_id not null,            /*部门负责人*/       
        department_status            kind NOT NULL                 /*部门状态*/
      

  3.   

    我也需要
    ==============
    谁指导一下viewtree&数据库操作?我想从数据库中调出数据形成树,一级目录已经实现,怎样实现第二层?addchild?哪位大哥给个例子学习学习,或者有这方面的详细资料也行啊![email protected],谢谢
      

  4.   

    呵呵,很简单啊,我昨天花了不到10分钟就搞定。唯一的区别是从INI文件里取数据,如
    [文档总类]
    内容=办公文件,小说
    [办公文件]
    内容=word文档,pdf文档,excel文档
    [word文档]
    ..........
    其中readini函数是读取标记显示内容
    splitstring是根据分隔符','取内容
    换成数据库记录集完全一样道理procedure ttools.InitlizeTree(flagstr:string;ParentNode: TTreeNode);
    var ss:tstringlist;
    i:integer;
     Nodx,FirstNode:TTreeNode   ;
    begin
      ss:=splitstring(readini(flagstr),',') ;
      if ss[0]='' then exit;
      for i:=0 to ss.Count-1 do
      begin
          nodx:=form1.TreeView1.Items.AddChild(ParentNode, ss[i]); //加载下级
          InitlizeTree(ss[i],nodx);   //递归调用
      end;
    end;
      

  5.   

    可以把表改造为这样的
        ID                           varchar(1000),
        PID                          varchar(97),
        department                   name NOT NULL,                /*部门号*/
        department_name              full_name NOT NULL,           /*部门名称 */
        parent_id                    name NOT NULL,                /*上级部门名称 */
        description                  information not null,         /*部门描述*/ 
        principal_staff              staff_id not null,            /*部门负责人*/       
        department_status            kind NOT NULL                 /*部门状态*/然后以ID,PID的关系表现树型关系,再以ID,PID的关系建树
      

  6.   

    参考
      while not datamodule2.tree.Eof do
        begin
         with tv.Items do
      begin
         MyTreeNode1 := Add(nil,trim(datamodule2.tree.fields[0].asstring));
          with datamodule2 do
          begin
           child.close;
           child.SQL.Clear;
           child.SQL.Add('select comp from dalei where plat='''+Trim(datamodule2.tree.Fields[0].asstring)+''''+' order by comp');
           child.Active:=true;
           child.Open;
           While Not child.Eof do
           begin
                TV.Items.AddChild(mytreenode1,trim(child.Fields[0].AsString));
                child.Next;
                end; end;
           datamodule2.tree.Next;
       end;
       end;
      

  7.   

    不知有用否﹐以前寫的﹐亂貼一下﹐樓主末怪
    procedure TForm1.openCDS;
    begin
    with c1 do begin
      Close;
      CommandText :='set nocount on'
        +' declare @a table (ii int identity(1,1),bom_id char(40),pds_id char (40),parentid int)'
        +' insert @a values('''','''+edit1.text+''',0)'
        +' declare @ii int'
        +' declare @maxii int'
        +' set @ii=0'
        +' set @maxii=1'
        +' while @ii<=@maxii'
        +' begin'
        +'   insert @a select  b.bom_id,b.pds_id,a.ii from @a a,BOM b where a.pds_id=b.bom_id and a.ii=@ii'
        +'   set @maxii=@maxii+@@rowcount'
        +'   set @ii=@ii+1'
        +'   if @maxii>500 break'
        +' end'
        +' select * from @a'
        +' set nocount off';
      Open;
    end;
    end;procedure TForm1.Button1Click(Sender: TObject);
    var
    p,t:TTreeNode;
    pdsList:TStringList;
    i,j:DWord;
    id:integer;
    sBom,sPds:string;
    begin
    i:=gettickcount;
    c1.DisableControls;
    pdsList:=TStringList.Create;
    try
      openCDS;
      j:=gettickcount;
      c1.First;
      t1.Items.Clear;  sPds:=trim(c1.fieldbyname('pds_id').AsString);
      t:=t1.Items.Add(nil,sPds);
      pdsList.AddObject('1',t);
      p:=t;
      c1.Next;
      while not c1.Eof do begin
    //    sBom:=trim(c1.FieldByName('bom_id').AsString);
        sPds:=trim(c1.fieldbyname('pds_id').AsString);
        id:=c1.fieldbyname('parentid').AsInteger;
        if p.Text <>sBom then begin
          p:=TTreeNode(pdsList.objects[pdsList.indexof(inttostr(id))]);
          if p=nil then begin
            showMessage('Error!');
            abort;
          end;
        end;    t:=t1.Items.AddChild(P,sPds);
        pdsList.AddObject(c1.fieldbyname('ii').asstring,t);
        c1.Next;
      end;
      t1.FullExpand;
    finally
      c1.EnableControls;
      pdsList.free;
    end;
    //showmessage(inttostr(j-i)+'+'+inttostr(gettickcount-j));
    end;
      

  8.   

    不知有用否﹐以前寫的﹐亂貼一下﹐樓主末怪
    procedure TForm1.openCDS;
    begin
    with c1 do begin
      Close;
      CommandText :='set nocount on'
        +' declare @a table (ii int identity(1,1),bom_id char(40),pds_id char (40),parentid int)'
        +' insert @a values('''','''+edit1.text+''',0)'
        +' declare @ii int'
        +' declare @maxii int'
        +' set @ii=0'
        +' set @maxii=1'
        +' while @ii<=@maxii'
        +' begin'
        +'   insert @a select  b.bom_id,b.pds_id,a.ii from @a a,BOM b where a.pds_id=b.bom_id and a.ii=@ii'
        +'   set @maxii=@maxii+@@rowcount'
        +'   set @ii=@ii+1'
        +'   if @maxii>500 break'
        +' end'
        +' select * from @a'
        +' set nocount off';
      Open;
    end;
    end;procedure TForm1.Button1Click(Sender: TObject);
    var
    p,t:TTreeNode;
    pdsList:TStringList;
    i,j:DWord;
    id:integer;
    sBom,sPds:string;
    begin
    i:=gettickcount;
    c1.DisableControls;
    pdsList:=TStringList.Create;
    try
      openCDS;
      j:=gettickcount;
      c1.First;
      t1.Items.Clear;  sPds:=trim(c1.fieldbyname('pds_id').AsString);
      t:=t1.Items.Add(nil,sPds);
      pdsList.AddObject('1',t);
      p:=t;
      c1.Next;
      while not c1.Eof do begin
    //    sBom:=trim(c1.FieldByName('bom_id').AsString);
        sPds:=trim(c1.fieldbyname('pds_id').AsString);
        id:=c1.fieldbyname('parentid').AsInteger;
        if p.Text <>sBom then begin
          p:=TTreeNode(pdsList.objects[pdsList.indexof(inttostr(id))]);
          if p=nil then begin
            showMessage('Error!');
            abort;
          end;
        end;    t:=t1.Items.AddChild(P,sPds);
        pdsList.AddObject(c1.fieldbyname('ii').asstring,t);
        c1.Next;
      end;
      t1.FullExpand;
    finally
      c1.EnableControls;
      pdsList.free;
    end;
    //showmessage(inttostr(j-i)+'+'+inttostr(gettickcount-j));
    end;