TREEVIEW先显示一个组织结构表如图,再在组织结构的末端部门下显该部门的员工名数据表,来实现末端部门显示员工名,如:工程部A下有成飞飞,张明明等,人事部下面有张三、李四等,我想,如果能取得有多少末端,和每个末端的位置,基本能解决问题,不知应怎么做为好?
显示组织结构是循环一个表: AddClass(0,nil); 
procedure TForm1.AddClass(AId: integer;FatherNode:TTreeNode);
var
    QryTmp:TADOQuery;
    myNode:TTreeNode;
    myLabel:TLabel;
   NewNode: TCPCCheckBoxNode;
begin
    QryTmp:=TADOQuery.Create(self);
    QryTmp.ConnectionString:='Provider=Microsoft.Jet.OLEDB.4.0;Data Source='+extractfilepath(application.ExeName)+'test.mdb;Persist Security Info=False';
    QryTmp.SQL.Add('select * from tb1');
    QryTmp.SQL.Add('where FatherId='+inttostr(AId));
    QryTmp.Open;
    while not QryTmp.Eof do
    begin
        myNode:=Treeview1.Items.AddChild(FatherNode,QryTmp.fieldbyname('CName').AsString);
        //创建标签,caption存放各分支的AutoId表识
        myLabel:=TLabel.Create(self);
        myLabel.Visible:=false;
        myLabel.Caption:=QryTmp.fieldbyname('AutoId').AsString;        myNode.Data:=myLabel;
        AddClass(QryTmp.fieldbyname('AutoId').AsInteger,myNode); //递归调用过程
        //NewNode:=TreeView1.AddCheckBoxNode(FatherNode,'5727');
        QryTmp.Next;
    end;
    QryTmp.Free;
end;如图:http://bbs.2ccc.com/attachments/2006/xbbtzhao_200662382040.jpg

解决方案 »

  1.   

    你可以把组织结构表和员工表组织成一个树图视图,再遍历。
    树图结构 
       ID          Name        ParentID
        1          工程部          0
        3          张x             1
      

  2.   

    你这个类似无级数,可以用楼上的方法来控制节点ParentID
      

  3.   

    用树图视图时:这句就出错了
     AddClass(QryTmp.fieldbyname('AutoId').AsInteger,myNode); //递归调用过程
      

  4.   

    提示什么错误??
    procedure TForm1.AddClass(AId: integer;FatherNode:TTreeNode);
    过程写对了吗?
      

  5.   

    不复杂的问题被人为复杂1、通过Data存储关键信息,譬如编号等
    2、通过遍历模式查找即可如果不是多级部门,直接通过遍历一级即可for I:=0 to TreeView.Items.Count-1 do
    begin
      P(TreeView.Items.Item[I].Data).
      //具体看Delphi关于Data的例子即可
    end;
      

  6.   

    楼上的朋友,能不能知道TreeView有多少个最后一项,并取得位置,因为我要一部门(是一个表)最后一项添加员工(是另一个表),这样可以遍历每一个最后一项并在下面加员工名?
      

  7.   

    当然可以啊,方法就多了1、如果没有子项,那是最后一项,这个判断对吧?
    2、当你添加了员工后,最后一级实际上多了子级这个对吧。
    完全可以通过Data控制啊,这个时候主要是为了控制没有员工的最后一级和员工区分,指针变量里面加一个项目就得了,具体看DATA帮助
      

  8.   

    Data控制我还不知很子解,楼上的朋友有没有说明和例子,,再次感谢你
      

  9.   

    完了,CSDN的提问越来越没水平了我上面已经说的很清楚了,看DELPHI的帮助,干脆把帮助贴出来得了
    The following code defines a record type of TMyRec and a record pointer type of PMyRec.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;