啊,我是个菜鸟,这些哪懂啊,郁闷啊,又有一个问题 delhpi中用BDGRID显示数据库内容的时候,想实现鼠标点击字段名就可以直接对这个字段进行排序,
我抄了下面的代码
procedure TForm3.DBGrid1TitleClick(Column: TColumn);
var
x:integer;
thefield:string; begin
x:=column.index;
thefield:=query1.Fields[x].fieldname;
with query1 do begin
active:=false;
sql.clear;
sql.Add('select * form gongzixitong order by'+thefield);
active:=true;
end;
end;
可是运行后,他出现这样的错误:
project mdiapp.exe raise exception class elisterror with message 'list index out of bounds(2)'.process stopped, use step or run to continue 请教是什么问题?
我抄了下面的代码
procedure TForm3.DBGrid1TitleClick(Column: TColumn);
var
x:integer;
thefield:string; begin
x:=column.index;
thefield:=query1.Fields[x].fieldname;
with query1 do begin
active:=false;
sql.clear;
sql.Add('select * form gongzixitong order by'+thefield);
active:=true;
end;
end;
可是运行后,他出现这样的错误:
project mdiapp.exe raise exception class elisterror with message 'list index out of bounds(2)'.process stopped, use step or run to continue 请教是什么问题?
与数据表中的字段没有对应
the list index of DBGrid is overflowed.
try
IntIdx := Column.Index;
StrOrder := Query1.Fields[IntIdx].Fieldname;
with Query1 do
begin
Active := False;
SQL.clear;
SQL.Add('SELECT * FROM tYourTable ORDER BY ' + StrOrder);
active := True;
end;
except
end;
end;If you do not have "try...except...end",please make sure that you have OPENED the Query before you click the title !! Then the error will never raise: 'list index out of bounds(2)'
begin
cname:=column.FieldName;
adoquery1.Close;
adoquery1.SQL.Clear;
adoquery1.SQL.Add('select * from test1 order by '+cname);
adoquery1.Open;
end;
我试过了,可以的,try
我只会瞎说
试试在sql 语句中显式指定字段名
select field1, field2... from table ...