I
Ivan_pan
Unregistered / Unconfirmed
GUEST, unregistred user!
我想用treeview控件动态地把数据库里的数据树型显示,但出现了问题!
我的数据库是这样的建表的:
create database medicin;
use medicin;
create table catagory(
id int(4) auto_increment not null,
name varchar(30) not null,
type int(4) not null,
primary key id
);
具体显示:
id name fatherid
1 A -1
2 B -1
3 C 1
4 D 1
5 E C
6 F 2
7 G 3
8 H 6
9 J 6
10 K 7
我的程序是这样写的:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ComCtrls, DB, ADODB;
type
TForm1 = class(TForm)
ADOConnection1: TADOConnection;
TreeView1: TTreeView;
ADOQuery1: TADOQuery;
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
procedure getalltypes(currentnode:ttreenode;fatherid:integer);
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure tform1.getalltypes(currentnode:ttreenode;fatherid:integer);
var
i:integer;
temnode:ttreenode;
begin
ADOQuery1.close;
adoquery1.sql.text:=format('select * from catagory where fatherid=''%d''',[fatherid]);
adoquery1.open;
if adoquery1.RecordCount<>0 then
begin
while not adoquery1.eof do
begin
temnode:=treeview1.items.addchild(currentnode,adoquery1.fieldbyname('name').value);
getalltypes(temnode,adoquery1.fieldbyname('id').value);
adoquery1.next;
end;
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
var
currentnode:ttreenode;
begin
treeview1.items.clear;
treeview1.items.add(treeview1.selected,'根目录');
currentnode:=treeview1.items.item[0];
currentnode.selectedindex:=0;
getalltypes(currentnode,-1);
end;
end.
end.
我用了一个递归想把所有的界点都显示出来(首先根节点是'根目录',然后father=-1得就是第二层节点,然后递归下去)
但显示的是:
根目录
A
C
G
其他的没有了,我想了一下是因为每次循环ADOQUERY1的数据源都会改变,所以我就想动态地每一次循环就添加一个ADOQUERY,
那我就想到用原生的ADO对象,于是程序变成:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ComCtrls, DB, ADODB;
type
TForm1 = class(TForm)
TreeView1: TTreeView;
procedure FormCreate(Sender: TObject );
procedure getalltypes(currentnode:ttreenode;fatherid:integer);
private
{ Private declarations }
adoc : Variant;
adocmd : Variant;
ador : Variant;
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
uses ComObj;
const
adCmdText = $00000001;
adOpenStatic = $00000003;
adUseClient = $00000003;
adLockOptimistic = $00000003;
procedure tform1.getalltypes(currentnode:ttreenode;fatherid:integer);
var
i:integer;
temnode:ttreenode;
begin
adoc := CreateOleObject('ADODB.Connection');
adoc.Open ('Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=Medecin;Data Source=PAN');
ador := CreateOleObject('ADODB.RecordSet');
ador.ActiveConnection := adoc;
ador.Open(format('select * from catagory where id=''%d''',[id]), adoc, adOpenStatic, adLockOptimistic, adCmdText);
if not ador.eof do
begin
temnode:=treeview1.items.addchild(currentnode,ador.fields['name'].value);
getalltypes(temnode,ador1.fields['id'].value);
ador.movenext;
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
var
currentnode:ttreenode;
begin
treeview1.items.clear;
treeview1.items.add(treeview1.selected,'根目录');
currentnode:=treeview1.items.item[0];
currentnode.selectedindex:=0;
getalltypes(currentnode,-1);
end;
end.
end.
运行时出现了eof 或 bof有一个为真的错误!
请问大家还有什么更好的办法???
小弟感谢不尽!
我的数据库是这样的建表的:
create database medicin;
use medicin;
create table catagory(
id int(4) auto_increment not null,
name varchar(30) not null,
type int(4) not null,
primary key id
);
具体显示:
id name fatherid
1 A -1
2 B -1
3 C 1
4 D 1
5 E C
6 F 2
7 G 3
8 H 6
9 J 6
10 K 7
我的程序是这样写的:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ComCtrls, DB, ADODB;
type
TForm1 = class(TForm)
ADOConnection1: TADOConnection;
TreeView1: TTreeView;
ADOQuery1: TADOQuery;
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
procedure getalltypes(currentnode:ttreenode;fatherid:integer);
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure tform1.getalltypes(currentnode:ttreenode;fatherid:integer);
var
i:integer;
temnode:ttreenode;
begin
ADOQuery1.close;
adoquery1.sql.text:=format('select * from catagory where fatherid=''%d''',[fatherid]);
adoquery1.open;
if adoquery1.RecordCount<>0 then
begin
while not adoquery1.eof do
begin
temnode:=treeview1.items.addchild(currentnode,adoquery1.fieldbyname('name').value);
getalltypes(temnode,adoquery1.fieldbyname('id').value);
adoquery1.next;
end;
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
var
currentnode:ttreenode;
begin
treeview1.items.clear;
treeview1.items.add(treeview1.selected,'根目录');
currentnode:=treeview1.items.item[0];
currentnode.selectedindex:=0;
getalltypes(currentnode,-1);
end;
end.
end.
我用了一个递归想把所有的界点都显示出来(首先根节点是'根目录',然后father=-1得就是第二层节点,然后递归下去)
但显示的是:
根目录
A
C
G
其他的没有了,我想了一下是因为每次循环ADOQUERY1的数据源都会改变,所以我就想动态地每一次循环就添加一个ADOQUERY,
那我就想到用原生的ADO对象,于是程序变成:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ComCtrls, DB, ADODB;
type
TForm1 = class(TForm)
TreeView1: TTreeView;
procedure FormCreate(Sender: TObject );
procedure getalltypes(currentnode:ttreenode;fatherid:integer);
private
{ Private declarations }
adoc : Variant;
adocmd : Variant;
ador : Variant;
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
uses ComObj;
const
adCmdText = $00000001;
adOpenStatic = $00000003;
adUseClient = $00000003;
adLockOptimistic = $00000003;
procedure tform1.getalltypes(currentnode:ttreenode;fatherid:integer);
var
i:integer;
temnode:ttreenode;
begin
adoc := CreateOleObject('ADODB.Connection');
adoc.Open ('Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=Medecin;Data Source=PAN');
ador := CreateOleObject('ADODB.RecordSet');
ador.ActiveConnection := adoc;
ador.Open(format('select * from catagory where id=''%d''',[id]), adoc, adOpenStatic, adLockOptimistic, adCmdText);
if not ador.eof do
begin
temnode:=treeview1.items.addchild(currentnode,ador.fields['name'].value);
getalltypes(temnode,ador1.fields['id'].value);
ador.movenext;
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
var
currentnode:ttreenode;
begin
treeview1.items.clear;
treeview1.items.add(treeview1.selected,'根目录');
currentnode:=treeview1.items.item[0];
currentnode.selectedindex:=0;
getalltypes(currentnode,-1);
end;
end.
end.
运行时出现了eof 或 bof有一个为真的错误!
请问大家还有什么更好的办法???
小弟感谢不尽!