关于Grid Index Out Of Range 的问题
sleaf 2009-02-16 10:16:07 我在窗体中添加了一个PageContol组件,通过切换PageContol的页面来实现不同数据表的全部记录显示,代码如下
procedure TForm2.PageControl1Change(Sender: TObject);
begin
if pagecontrol1.TabIndex=0 then
begin
datamodule1.ADOQuery2.Close;
with datamodule1.ADOQuery1 do
begin
close;
sql.Clear;
sql.Add('select * from 年度培训计划表 order by 年度, 编号 ASC');
open;
last;
end;
DBGrid1.DataSource:=datamodule1.DataSource1;
DBNavigator1.DataSource:=datamodule1.DataSource1;
end;
if pagecontrol1.TabIndex=1 then
begin
datamodule1.ADOQuery1.Close;
with datamodule1.ADOQuery2 do
begin
close;
sql.Clear;
sql.Add('select * from 计划外培训项目表 order by 编号 ASC');
open;
last;
end;
DBGrid1.DataSource:=datamodule1.DataSource2;
DBNavigator1.DataSource:=datamodule1.DataSource2;
end;
end;
窗体显示时Pagecontrol的Tabindex为0,代码如下:
procedure TForm2.FormShow(Sender: TObject);
begin
dbgrid1.DataSource:=datamodule1.DataSource1;
dbnavigator1.DataSource:=datamodule1.DataSource1;
with datamodule1.ADOQuery1 do
begin
close;
sql.Clear;
sql.Add('select * from 年度培训计划表 order by 年度, 编号 ASC');
open;
last;
end;
end;
然后我又增加了一个按钮,点击时显示表的全部记录,代码如下:
procedure TForm2.QueryAllClick(Sender: TObject);
begin
if pagecontrol1.TabIndex=0 then
begin
datamodule1.ADOQuery2.Close;
with datamodule1.ADOQuery1 do
begin
close;
sql.Clear;
sql.Add('select * from 年度培训计划表 order by 年度, 编号 ASC');
open;
last;
end;
DBGrid1.DataSource:=datamodule1.DataSource1;
DBNavigator1.DataSource:=datamodule1.DataSource1;
end;
if pagecontrol1.TabIndex=1 then
begin
datamodule1.ADOQuery1.Close;
with datamodule1.ADOQuery2 do
begin
close;
sql.Clear;
sql.Add('select * from 计划外培训项目表 order by 编号 ASC');
open;
last;
end;
DBGrid1.DataSource:=datamodule1.DataSource2;
DBNavigator1.DataSource:=datamodule1.DataSource2;
end;
end;
现在的问题在于,当窗体显示时,我点击该按钮,就会出错,弹出出现异常:Grid index out of range 。
利用F7跟踪调试,发现错误在last这条语句上。
我搞了半天也没弄明白为什么会出错,敬请大家赐教!!!!!!