想用StringGrid来显示数据库中的内容,但是出了错误:(33分)

  • 主题发起人 crystal_bobo
  • 开始时间
C

crystal_bobo

Unregistered / Unconfirmed
GUEST, unregistred user!
我想用StringGrid来显示数据库中的内容,但是出了错误:

我放置了一个ADOConnection组件,连接好了Tiyuzu数据库
然后放置一个ADOQuery数据库,连接上ADOConnection,并在ADOQuery的SQL上写上:select * from nan_one
放置一个StringGrid,代码:
procedure TForm1.StringGrid1Click(Sender: TObject);
begin
with ADoQuery1 do
begin
Close;
Sql.Clear;
Sql.add('Select * from nan_one');
open;
TstringGrid1.RowCount := ADoQuery1.RecordSet.count;
TstringGrid1.ColCount := ADoQuery1.Fields.Count;
j := 0;
while not eof do
begin
for i := 0 to TstringGrid1.ColCount-1 do
begin
TstringGrid1.Cells[i,j] := Fields.asstring;
end;
inc(J);
ADoQuery1.next;
end;
end;

end;
显示出了很多的错误
请帮忙看看,怎么改过来呢?
[Error] Unit1.pas(36): Undeclared identifier: 'TstringGrid1'
[Error] Unit1.pas(36): Missing operator or semicolon
[Error] Unit1.pas(36): Undeclared identifier: 'count'
[Error] Unit1.pas(37): Missing operator or semicolon
[Error] Unit1.pas(38): Undeclared identifier: 'j'
[Error] Unit1.pas(41): Undeclared identifier: 'i'
[Error] Unit1.pas(41): 'DO' expected but identifier 'ColCount' found
[Error] Unit1.pas(43): Missing operator or semicolon
[Fatal Error] Project2.dpr(5): Could not compile used unit 'Unit1.pas'

请帮帮忙,初次接触ADOQuery和StringGrid的使用
 
Undeclared identifier 没有声明
先把你用到的变量都声明一下
 
算了,你连基本的都没搞清楚。你的变量定义了没有?
 
procedure TForm1.StringGrid1Click(Sender: TObject);
var
StringGrid1 :TStringGrid;
begin
with ADoQuery1 do
begin
Close;
Sql.Clear;
Sql.add('Select * from nan_one');
open;
StringGrid1.RowCount := ADoQuery1.RecordSet.count;
StringGrid1.ColCount := ADoQuery1.Fields.Count;
j := 0;
while not eof do
begin
for i := 0 to StringGrid1.ColCount-1 do
begin
StringGrid1 .Cells[i,j] := Fields.asstring;
end;
inc(J);
ADoQuery1.next;
end;
end;

end;
显示出了很多的错误
请帮忙看看,怎么改过来呢?
[Error] Unit1.pas(36): Undeclared identifier: 'TstringGrid1'
[Error] Unit1.pas(36): Missing operator or semicolon
[Error] Unit1.pas(36): Undeclared identifier: 'count'
[Error] Unit1.pas(37): Missing operator or semicolon
[Error] Unit1.pas(38): Undeclared identifier: 'j'
[Error] Unit1.pas(41): Undeclared identifier: 'i'
[Error] Unit1.pas(41): 'DO' expected but identifier 'ColCount' found
[Error] Unit1.pas(43): Missing operator or semicolon
[Fatal Error] Project2.dpr(5): Could not compile used unit 'Unit1.pas'
 
procedure TForm1.StringGrid1Click(Sender: TObject);
var
i, j: Integer;
begin
with Query1 do begin
Open;
First;
StringGrid1.ColCount:= FieldCount+1;
StringGrid1.RowCount:= RecordCount+1;
for i:=0 to RecordCount-1 do
begin
for j:=0 to FieldCount-1 do
begin
StringGrid1.Cells[j+1,i+1]:= Fields[j].AsString;
end;
Next;
end;
end;
end;

BTW:最好先看一下书,把基本概念弄清楚
 
声明之后还是还是显示:
Missing operator or semicolon
 
而且你为什么要在StringGrid的OnClick事件中进行数据填充?这样不是每点一下StringGrid
就会触发一次?
最好写在按钮的OnClick事件中,或者写在Form的OnShow事件中
 
顶部