树形结构加载问题(请检查代码)(20分)

  • 主题发起人 主题发起人 j76
  • 开始时间 开始时间
J

j76

Unregistered / Unconfirmed
GUEST, unregistred user!
我用ADO数据控件(代码如下)unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, DB, ADODB, ComCtrls;

type
TForm1 = class(TForm)
Tree: TTreeView;
ADOConnection1: TADOConnection;
ADODataSet1: TADODataSet;
private
{ Private declarations }
public
Procedure LoadTree(treeDB:TADODataSet);
function GetNodeLevel(sFormat,sCode:string):integer;
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

{ TForm1 }
const
cTreeCodeFormat='122222';
cTreeMaxLevel=6;
cTreeRootTxt='单位字典';

procedure tform1.LoadTree(treeDB:TADODataSet);//初始化树
//initial tree when main form create
var curID,nodeTxt:string;
level,chindex,cnode,num:integer;
mynode:array[0..6] of TTreenode;
begin
//ini value
Screen.Cursor:=crHourGlass;
tree.Enabled:=True;
tree.Items.Clear;
level:=0 ;
num:=1;
tree.items.clear;
mynode[level]:=tree.items.add(Tree.Topitem,cTreeRootTxt);

with treeDB do
begin
try
if not Active then open;
first;
while not Eof do
begin
curID:=trim(FieldByName('id').AsString);
nodeTxt:=curID+'-'+trim(FieldByName('name').AsString);
level:=GetNodeLevel(cTreeCodeFormat,curID);
if level>0 then
begin
mynode[level]:=tree.items.addchild(mynode[level-1],nodeTxt);

end;
next;
end;
finally;
close;
End;
mynode[0].expand(False);
Screen.Cursor:=crDefault;
end;
end;

function TForm1.GetNodeLevel(sFormat,sCode:string):integer;
var i,level,iLen:integer;
begin
level:=-1 ;
iLen:=0;
if (sFormat<>'') and (sCode<>'') then
for i:=1 to Length(sFormat) do
begin
iLen:=iLen+StrToInt(sFormat);
if Length(sCode)=iLen then
begin
level:=i;
break;
end;
end;
result:=level;
end;

end.
为什么不能加在到TREEVIEW上
 
数据库结构如下
ID NAME
1 ****
101 ****
10101 ****
 

Similar threads

I
回复
0
查看
503
import
I
S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
I
回复
0
查看
588
import
I
后退
顶部