首页  编辑  

数据集和TreeView

Tags: /超级猛料/Database.数据库相关/数据库控件/   Date Created:

procedure LoadTree(treeDB:TDBDataSet);// 初始化树

   procedure UpdateTree(curNode:TTreenode; nodeTxt:string; state:string);//更新树

   function  GetNodeLevel(sFormat,sCode:string):integer; //获得节点层数

   function  GetNodeItem(sCode:string):integer;//获得item

   { Public declarations }

 end;

   const

 CTreeCodeFormat='122222';

 cTreeMaxLevel=6;

 CTreeRootTXT='所有图书';

var

 tsgzlfrom: Ttsgzlfrom;

 _err:integer;

 curUser:string[10];

 mystate:string;

 gNodeId:string;

 gNodelevel:integer;

 gNode:TtreeNode;

 mynode:array[0..6] of TTreenode;

 i,Already,CurMode:integer;

 currow:integer;

 iniFile:string;

 HasSub:String;

 level:Integer;

 implementation

uses bgNewunit;

{$R *.DFM}

procedure TtsgzlFrom.LoadTree(treeDB:TDBDataSet);//初始化树

var curID,nodeTxt:string;

   level,num:integer;

begin

   //初始化变量

   Screen.Cursor:=crHourGlass;

   tree.Enabled:=True;

   tree.Items.Clear;

   level:=1 ;

   num:=1;

   tree.items.clear;

   //设置根节点

   mynode[level]:=Tree.items.add(Tree.Topitem,cTreeRootTxt);

   mynode[level].ImageIndex:=0;

   mynode[level].SelectedIndex:=1;

   //遍历数据表,利用编码字段记录排序规律,依次添加树节点

   with TreeDb do

   begin

      try

      if not Active then open;

      first;

      while not Eof do

      begin

        curID:=trim(FieldByName('tsglb').AsString);

        nodeTxt:=curID+'-'+trim(FieldByName('tsglbn').AsString);

        level:=GetNodeLevel(cTreeCodeFormat,curID);

        //这里返回代码的层次数

        if level>0 then

        begin

 //增加下一节点时,用添加子节点的方法可轻松实现节点间的层次关系。

        //注意:这里的父节点是用当前节点的上一级节点mynode[level-1]

           mynode[level]:=Tree.Items.AddChild(Mynode[level-1],NodeTxt);

           mynode[level].ImageIndex:=2;

           mynode[level].SelectedIndex:=3;

        end;

        next;//下一条记录

      end;

      finally;

        close;

      End;

      mynode[1].expand(False);

      Screen.Cursor:=crHourGlass;

    end;

end;

function TtsgzlFrom.GetNodeLevel(sFormat,sCode:string):integer;

var i,iLen:integer;

begin

 level:=-1  ;

 iLen:=0;

 if (sFormat<>'') and (sCode<>'') then

   for i:=1 to Length(sFormat) do //分析编码格式,找出当前代码层次

   begin

     iLen:=iLen+StrToInt(sFormat[i]);

     if Length(sCode)=iLen then

     begin

       level:=i;

       break;

     end;

   end;

 result:=level;

end;

Function TtsgzlFrom.GetNodeItem(sCode:string):integer;//获得item

var i,iCount,val:integer;

   tmp:string;

begin

 Result:=0;

 iCount:=Tree.Items.Count;

 if iCount=0 then exit;

 val:=0;

 for i:=1 to iCount-1 do

 begin

    Tmp:=Tree.Items.Item[i].Text;

    Tmp:=Copy(Tmp,0,pos('-',Tmp)-1);

    if Tmp=sCode then begin

       val:=i;

       Break;

    end;

 end;

 result:=val;

end;

procedure TtsgzlFrom.UpdateTree(curNode:TTreenode; nodeTxt:string; state:string);

Begin

 if  state='add'   then

   begin

     curNode:=Tree.Items.addchild(curNode,nodeTxt);

     curNode.ImageIndex:=2;

     curnode.SelectedIndex:=3;

   end;

 if state='del' then  curNode.delete;

 if state='edi' then  curNode.Text:=nodeTxt;

end;