加载树形结构

表SoftClass:
SID   编号 自动编号
ATEXT 文字 字符
PID 父节点 数字

type //软件分类结构
 PSoftClass=^TSoftClass;
 TSoftClass=record
  SID:String; //软件分类编号
  SoftText:String;//分类文字
  PID:String;//父编号
 end;

//查询,返回成功或失败
function GetRecordSet(var adoQry:TADOQuery;strSQL:String):Boolean;
//加载软件分类
procedure LoadSoftClass(adoQry:TADOQuery;tv:TTreeView;var PSoft:array of PSoftClass);
//根据父节点加载
function LoadNodeFromParent(tv:TTreeView;var PSoft:array of PSoftClass;CurP:Integer;PID:String;ParentNode:TTreeNode;cdsTree:TClientDataset):Boolean;

//根据父节点加载
function LoadNodeFromParent(tv:TTreeView;var PSoft:array of PSoftClass;CurP:Integer;PID:String;ParentNode:TTreeNode;cdsTree:TClientDataset):Boolean;
var
 ret:Boolean;
 NewNode:TTreeNode;
 a:PString;
 cdsTmp:TClientDataset;
begin
  try
   cdsTmp:=TClientDataset.Create(nil);
   cdsTmp.Data:=cdsTree.Data;
   with cdsTmp do
   begin
     Filtered:=False;
     Filter:='PID='+PID;
     Filtered:=True;
     //First;
     while not eof do
     begin
       new(PSoft[CurP]);
       PSoft[CurP].SID:=FieldByName('SID').AsString;
       PSoft[CurP].SoftText:=FieldByName('AText').AsString;
       PSoft[CurP].PID:=FieldByName('PID').AsString;
       NewNode:=tv.Items.AddChildObject(ParentNode,PSoft[CurP].SoftText,TObject(PSoft[CurP]));
       if not (ParentNode=nil) then ParentNode.Expand(True);
       Inc(CurP);
       ret:=LoadNodeFromParent(tv,Psoft,CurP,FieldByName('SID').AsString,NewNode,cdsTmp);
       next;
     end;//while
     Filtered:=False;
   end;//with
  finally
   cdsTmp.Free;
  end;//try
  result:=ret;
end;


//加载软件分类
procedure LoadSoftClass(adoQry:TADOQuery;tv:TTreeView;var PSoft:array of PSoftClass);
var
  I:Integer;
  cdsTmp:TClientDataset;
begin
  tv.Items.Clear;
  try
   cdsTmp:=TClientDataset.Create(nil);
   with cdsTmp.FieldDefs do
   begin
    add('SID', ftInteger); //序号
    add('AText', ftString, 50, false); //记录值
    add('PID', ftInteger); //序号
   end;//with
  cdsTmp.CreateDataSet;
  with adoQry do
  begin
   if not IsEmpty then
   begin
    First;
    while not eof do
    begin
     cdsTmp.Append;
     cdsTmp.FieldByName('SID').AsInteger:=Fieldbyname('SID').AsInteger;
     cdsTmp.FieldByName('ATEXT').AsString:=FieldByName('ATEXT').AsString;
     cdsTmp.FieldByName('PID').AsInteger:=Fieldbyname('PID').AsInteger;
     cdsTmp.Post;
     Next;
    end;//while
   end;//if
  end;//with
  //加载根节点
   I:=0;
   LoadNodeFromParent(tv,PSoft,I,'-1',nil,cdsTmp);
  finally
   cdsTmp.Free;
  end;
end;


function GetRecordSet(var adoQry:TADOQuery;strSQL:String):Boolean;
begin
  Result:=False;
  try
   with adoQry do
   begin
    Connection:=DataM.adoConn;
    Close;
    SQL.Clear;
    SQL.Add(strSQL);
    //WriteLog(strSQL);
    Open;
   end;
   if not adoQry.IsEmpty then
     Result:=True;
  except
   //Result:=False;
  end;
end;

调用:
var
 PSoft:array of PSoftClass;
 adoQry:TADOQuery;

   adoQry:=TADOQuery.Create(nil);
   sSQL:='select * from SoftClass';
   if not GetRecordSet(adoQry,sSQL) then
    exit;
SetLength(PSoft,adoQry.RecordCount);
LoadSoftClass(adoQry,t,PSoft);

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值