F
fullstrong
Unregistered / Unconfirmed
GUEST, unregistred user!
我建有一个数据库文件,该库文件数据主要是用TreeView来进行显示,该库结构如下:
Parent SubKey Subject_Text
0 001 a
0 002 B
001 00101 C
002 00201 c1
0 003 A1
001 00102 D
在TreeView中显示的结构应为:
---树结构------
|
|---a
| |
| |---C
| |---D
|---B
| |
| |-C1
|
|---A1
我编写的代码如下:
type
PMyTree = ^TMyTree;
TMyTree = record //用于存放树节点的ID(subkey)
TreeSubKey: string;
end;
procedure TMainForm.FormCreate(Sender: TObject);
begin
//从库中调用数据到TreeView
WITH TblCategories do
begin
TblCategories.DatabaseName:=ParamStr(1); //指向应用程序所在路径
TblCategories.TableName:='Categories.db';
TblCategories.open;
end;
LoadCode(TblCategories);
end;
function TMainForm.LoadCode(crTbl:TDBDataSet):Integer;
var
ShowTxt:String;
Node:TtreeNode;
MyTreePtr: PMyTree;
Par_ID:String;
begin
New(MyTreePtr);
with crTbl do
begin
try
if not Active then Open;
First;
TreeView1.Items.Clear; //以下是增加第一项
Node:=TreeView1.Items.Add(TreeView1.TopItem,'类别树');
MyTreePtr^.TreeSubKey:='0';
node.data:=MyTreePtr;
While Not Eof do
begin
MyTreePtr^.TreeSubKey:=Trim(FieldByName('SubKey').AsString);
Par_ID:=Trim(FieldByName('Parent').AsString);
ShowTxt:=Trim(FieldByName('Subject_Text').AsString);
node:=FindParentNode(Par_ID);
node:=TreeView1.Items.AddChild(Node, ShowTxt);
node.data:=MyTreePtr;
Next;
end;
finally
Close;
end;
end;
end;
Function TMainForm.FindParentNode(Parent_ID:String):TtreeNode;
Var
i:integer;
TreePtr: PMyTree;
Parent_key:string;
begin
Result:=nil;
For i:=0 to TreeView1.Items.count-1 do
begin
TreePtr:=TreeView1.Items.Data;
Parent_Key:=Trim(TreePtr^.TreeSubKey);
if Parent_Key=Parent_ID then
begin
Result:=TreeView1.Items;
Break;
end;
end;
end;
结果在TreeView中显示的为下图:
---树结构------
|
|---a
|---C
|---D
|---B
|---C1
|---A1
不知为何成这样,难道是FindParentNode()有问题吗? 在下在这里请各位DELPHI高手帮我看看,
到底是何处出错了? 谢谢!!
Parent SubKey Subject_Text
0 001 a
0 002 B
001 00101 C
002 00201 c1
0 003 A1
001 00102 D
在TreeView中显示的结构应为:
---树结构------
|
|---a
| |
| |---C
| |---D
|---B
| |
| |-C1
|
|---A1
我编写的代码如下:
type
PMyTree = ^TMyTree;
TMyTree = record //用于存放树节点的ID(subkey)
TreeSubKey: string;
end;
procedure TMainForm.FormCreate(Sender: TObject);
begin
//从库中调用数据到TreeView
WITH TblCategories do
begin
TblCategories.DatabaseName:=ParamStr(1); //指向应用程序所在路径
TblCategories.TableName:='Categories.db';
TblCategories.open;
end;
LoadCode(TblCategories);
end;
function TMainForm.LoadCode(crTbl:TDBDataSet):Integer;
var
ShowTxt:String;
Node:TtreeNode;
MyTreePtr: PMyTree;
Par_ID:String;
begin
New(MyTreePtr);
with crTbl do
begin
try
if not Active then Open;
First;
TreeView1.Items.Clear; //以下是增加第一项
Node:=TreeView1.Items.Add(TreeView1.TopItem,'类别树');
MyTreePtr^.TreeSubKey:='0';
node.data:=MyTreePtr;
While Not Eof do
begin
MyTreePtr^.TreeSubKey:=Trim(FieldByName('SubKey').AsString);
Par_ID:=Trim(FieldByName('Parent').AsString);
ShowTxt:=Trim(FieldByName('Subject_Text').AsString);
node:=FindParentNode(Par_ID);
node:=TreeView1.Items.AddChild(Node, ShowTxt);
node.data:=MyTreePtr;
Next;
end;
finally
Close;
end;
end;
end;
Function TMainForm.FindParentNode(Parent_ID:String):TtreeNode;
Var
i:integer;
TreePtr: PMyTree;
Parent_key:string;
begin
Result:=nil;
For i:=0 to TreeView1.Items.count-1 do
begin
TreePtr:=TreeView1.Items.Data;
Parent_Key:=Trim(TreePtr^.TreeSubKey);
if Parent_Key=Parent_ID then
begin
Result:=TreeView1.Items;
Break;
end;
end;
end;
结果在TreeView中显示的为下图:
---树结构------
|
|---a
|---C
|---D
|---B
|---C1
|---A1
不知为何成这样,难道是FindParentNode()有问题吗? 在下在这里请各位DELPHI高手帮我看看,
到底是何处出错了? 谢谢!!