我用query条件查询,stringgrid为什么不能显示出根据条件查询出的记录?

ruyunluck 2002-08-31 12:43:01
在EDIT中输入编号,
根据编号进行查询,如果输入的编号存在就在stringgrid显示出来。否则显示没有
我的代码是:
procedure form1.button1click()
var
i,j:integer;
begin
with query1 do
begin
close;
sql.clear;
sql.add('select * from biao');
sql.add('where 编号=:mid');
parambyname('mid').asstring:=edit1.text;
open;
stringgrid1.rowcount:=query1.recordcount;
stringgrid1.colcount:=query1.fieldcount;
if table1.findkey([edit1.text]) then //table1 是biao
begin
j:=0;
while not eof do begin
for i:=0 to stringgrid1.colcount-1 do
stringgrid1.cells[i,j]:=query1.fields[i].asstring;
inc(j);
query1.next;
end;
end
else
showmessage('no find');
end;
end;


程序运行的问题是:
输入编号后,不能在stringgrid中显示出与编号相符合的记录。请问错在哪里?
...全文
99 12 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
ruyunluck 2002-08-31
  • 打赏
  • 举报
回复
非常的感谢您。
begin
stringgrid1.cells[i,j]:=query1.fields[i].asstring;
end;
Next;
inc(j);//在这儿不可以少的,而且要用begin..end括起来
用这个对了。

但是如果要增加显示字段名时情况同以前。

再次的感激你!祝你工作顺利
fuzhe2001 2002-08-31
  • 打赏
  • 举报
回复
一般的在stringgrid的最上面一行显示出字段名,最好亲自定义:如:
procedure Form1.FormShow(Sender: TObject);
begin
stringgrid1.Cells[0,0]:='编号';
stringgrid1.Cells[1,0]:='药品名称';
stringgrid1.Cells[2,0]:='规格';
stringgrid1.Cells[3,0]:='数量';
stringgrid1.Cells[4,0]:='数量';
stringgrid1.Cells[5,0]:='金额';

end;
一般是放在初始化中

同时:
procedure form1.button1click()
var
i,j:integer;
begin
with query1 do
begin
close;
sql.clear;
sql.add('select * from biao');
sql.add('where 编号=:mid');
parambyname('mid').asstring:=edit1.text;
open;
Last;
First;//如果是sql server的话,一定要这样的;
stringgrid1.rowcount:=query1.recordcount;
stringgrid1.colcount:=query1.fieldcount;
if table1.findkey([edit1.text]) then //table1 是biao
begin
J ;=1
while not eof do
begin
for i:=0 to stringgrid1.colcount-1 do
begin
stringgrid1.cells[i,j]:=query1.fields[i].asstring;
end;
Next;
inc(j);//在这儿不可以少的,而且要用begin..end括起来
end;
end else
showmessage('no find');
end;
end;
procedure form1.button1click()
var
i,j:integer;
begin
with query1 do
begin
close;
sql.clear;
sql.add('select * from biao');
sql.add('where 编号=:mid');
parambyname('mid').asstring:=edit1.text;
open;
Last;
First;//如果是sql server的话,一定要这样的;
stringgrid1.rowcount:=query1.recordcount;
stringgrid1.colcount:=query1.fieldcount;
if table1.findkey([edit1.text]) then //table1 是biao
begin
J ;=1 //j必须是1
while not eof do
begin
for i:=0 to stringgrid1.colcount-1 do
begin
stringgrid1.cells[i,j]:=query1.fields[i].asstring;
end;
Next;
inc(j);//在这儿不可以少的,而且要用begin..end括起来
end;
end else
showmessage('no find');
end;
end;


ihihonline 2002-08-31
  • 打赏
  • 举报
回复
procedure Ttdeform.DoYss;
var debhtemp,gcmc:ansistring;
desl:real;//定额数量
gcbh:integer;//工程编号
i,j:integer;
xj:real;//小结合计
hj:real;//合计
rghj,//人工合计
clhj,//材料合计
jxhj:real;//机械合计
rowcnt:integer;//计数器
GcNum:integer;//工程计数器
begin
with projectdatamodule.yssquery do //打开预算表
begin
Close;
SQl.Clear;
sql.Add('Select * from t_yss');
prepared;
open;
first;
end;
with projectdatamodule.yssquery2 do//打开临时预算表
begin
Close;
SQl.Clear;
sql.Add('Select * from yss');
prepared;
open;
if recordcount<>0 then //清除临时预算表数据
begin
for i:=1 to recordcount do delete;
UpdateBatch;
end;
end;
//复制数据从预算表到临时预算表
with projectdatamodule do
begin
for j:=1 to yssquery.recordcount do
begin
yssquery2.Append;
yssquery2.FieldByName('debh').AsString:=yssquery.FieldByName('debh').AsString;
yssquery2.FieldByName('demc').AsString:=yssquery.FieldByName('demc').AsString;
yssquery2.FieldByName('dw').AsString:=yssquery.FieldByName('dw').AsString;
yssquery2.FieldByName('sl').Asfloat:=yssquery.FieldByName('sl').Asfloat;
yssquery2.FieldByName('jj').Asfloat:=yssquery.FieldByName('jj').Asfloat;
yssquery2.FieldByName('jjhj').Asfloat:=yssquery.FieldByName('jjhj').Asfloat;
yssquery2.FieldByName('gcm').AsString:=yssquery.FieldByName('gcm').Asstring;
yssquery2.Updatebatch;
yssquery.Next;
end;
yssquery.first;
yssquery2.first;
end;
//清除stringgrid数据
for i:=0 to stringgrid1.RowCount do
for j:=0 to stringgrid1.ColCount do stringgrid1.Cells[j,i]:='';

//第二步、分小结显示定额
with rationdatamodule.gcquery do
begin
close;
sql.Clear;
sql.Add('Select * from t_gcsm');
prepared;
open;
first;
rowcnt:=0;
rghj:=0;
clhj:=0;
jxhj:=0;
hj:=0;
GcNum:=0;
for i:=1 to recordcount do
begin
gcmc:=trim(fieldbyname('gcm').AsString);
projectdatamodule.yssquery2.Close;
projectdatamodule.yssquery2.SQl.Clear;
projectdatamodule.yssquery2.sql.Add('Select * from yss');
projectdatamodule.yssquery2.sql.Add('where gcm =:gcm');
projectdatamodule.yssquery2.Parameters.ParamByName('gcm').value:=gcmc;
projectdatamodule.yssquery2.prepared;
projectdatamodule.yssquery2.open;
projectdatamodule.yssquery2.first;
if projectdatamodule.yssquery2.recordcount<>0 then//写数据到StringGrid
begin
GcNum:=GcNum+1;
StringGrid1.Cells[0,rowcnt]:=NumExchange(GcNum);
StringGrid1.Cells[1,rowcnt]:=gcmc;
stringgrid1.RowCount:=stringgrid1.RowCount+1;
rowcnt:=rowcnt+1;
xj:=0;
for j:=1 to projectdatamodule.yssquery2.recordcount do
begin
stringgrid1.Cells[0,rowcnt]:=inttostr(j);
stringgrid1.Cells[1,rowcnt]:=projectdatamodule.yssquery2.fieldbyname('demc').AsString;
stringgrid1.Cells[2,rowcnt]:=currtostr(projectdatamodule.yssquery2.fieldbyname('sl').Asfloat);
stringgrid1.Cells[3,rowcnt]:=projectdatamodule.yssquery2.fieldbyname('dw').AsString;
stringgrid1.Cells[4,rowcnt]:=format('%8.2f',[projectdatamodule.yssquery2.fieldbyname('jj').Asfloat]);
stringgrid1.Cells[5,rowcnt]:=format('%8.2f',[projectdatamodule.yssquery2.fieldbyname('jjhj').Asfloat]);
rghj:=rghj+projectdatamodule.yssquery2.fieldbyname('rghj').Asfloat;
clhj:=clhj+projectdatamodule.yssquery2.fieldbyname('clhj').Asfloat;
jxhj:=jxhj+projectdatamodule.yssquery2.fieldbyname('jxhj').Asfloat;
xj:=xj+projectdatamodule.yssquery2.fieldbyname('jjhj').Asfloat;
stringgrid1.RowCount:=stringgrid1.RowCount+1;
rowcnt:=rowcnt+1;
projectdatamodule.yssquery2.next;
end;
//小计
stringgrid1.cells[1,rowcnt]:='小计';
stringgrid1.cells[5,rowcnt]:=format('%8.2f',[xj]);
hj:=hj+xj;
stringgrid1.RowCount:=stringgrid1.RowCount+1;
rowcnt:=rowcnt+1;
end;
next;
end;
stringgrid1.RowCount:=stringgrid1.RowCount+1;
stringgrid1.cells[1,rowcnt]:='合计';
stringgrid1.cells[5,rowcnt]:=format('%8.2f',[hj]);
end;
end;
第一个项目时写的:)
ihihonline 2002-08-31
  • 打赏
  • 举报
回复
procedure form1.button1click()
var
i,j:integer;
begin
with query1 do
begin
close;
sql.clear;
sql.add('select * from biao');
sql.add('where 编号=:mid');
parambyname('mid').asstring:=edit1.text;
open;
Last;
First;//如果是sql server的话,一定要这样的;
stringgrid1.rowcount:=query1.recordcount;
stringgrid1.colcount:=query1.fieldcount;
if table1.findkey([edit1.text]) then //table1 是biao
begin
J ;=1
while not eof do
begin
for i:=0 to stringgrid1.colcount-1 do
begin
stringgrid1.cells[i,j]:=query1.fields[i].asstring;
end;
Next;
inc(j);//在这儿不可以少的,而且要用begin..end括起来
end;
end else
showmessage('no find');
end;
end;
//如果还不行的话,我给你例程吧;
我什么也没有,如何在机子上试?没有库、表
ruyunluck 2002-08-31
  • 打赏
  • 举报
回复
尊敬的朋友:
再次感谢你!
我是用sql server建的表 biao
biao 中有编号,药品名称,规格,数量,单价,金额等字段。

procedure form1.button1click()
var
i,j:integer;
begin
with query1 do
begin
close;
sql.clear;
sql.add('select * from biao');
sql.add('where 编号=:mid');
parambyname('mid').asstring:=edit1.text;
open;
Last;
First;//如果是sql server的话,一定要这样的;
stringgrid1.rowcount:=query1.recordcount;
stringgrid1.colcount:=query1.fieldcount;
if table1.findkey([edit1.text]) then //table1 是biao
begin
while not eof do
begin
J;=0;
for i:=0 to stringgrid1.colcount-1 do
begin
stringgrid1.cells[i,j]:=query1.fields[i].asstring;
inc(j);
end;
Next;
end;
end else
showmessage('no find');
end;
end;

然后我输入表中存在的编号,只在stringgrid的最上面一行的第一列显示出编号
值,其他列没有任何显示。

我注意到:当我把 for i:=0 to stringgrid1.colcount-1 do中的i:=0改为
i:=1。则只在stringgrid的最上面一行的第二列显示出药品名称值,其他列没有任何显示,在第一列也没有显示出输入的编号值。


同时,我希望能在stringgrid的最上面一行显示出字段名
所以我希望能够加入for k:=0 to query1.fieldcount-1 do
begin
stringgrid1.cells[k,0]:=query1.fields[i].fieldname;//显示字段名
end;
所以这时我把while not eof do
begin
J;=0;
中的j:=0改为j:=1;
加入这一段代码后程序运行时只能出字段名。不能显示出任何值。

麻烦你了!请你再看看或亲自上机试验好吗?
不胜感激
fuzhe2001 2002-08-31
  • 打赏
  • 举报
回复
不对吧,应该把inc(j)放在begin....end 的外面!
ihihonline 2002-08-31
  • 打赏
  • 举报
回复
procedure form1.button1click()
var
i,j:integer;
begin
with query1 do
begin
close;
sql.clear;
sql.add('select * from biao');
sql.add('where 编号=:mid');
parambyname('mid').asstring:=edit1.text;
open;
Last;
First;//如果是sql server的话,一定要这样的;
stringgrid1.rowcount:=query1.recordcount;
stringgrid1.colcount:=query1.fieldcount;
if table1.findkey([edit1.text]) then //table1 是biao
begin
//j:=0;
while not eof do
begin
J ;=1;//每次都进行初始休,明白?
for i:=0 to stringgrid1.colcount-1 do
begin
stringgrid1.cells[i,j]:=query1.fields[i].asstring;
inc(j);//在这儿不可以少的,而且要用begin..end括起来
end;
Next;
end;
end else
showmessage('no find');
end;
end;
Michaelyfj 2002-08-31
  • 打赏
  • 举报
回复
你没有符合这个编号的记录吧。
先查看记录数:
sql.clear;
sql.add('select * from biao');
sql.add('where 编号=:mid');
parambyname('mid').asstring:=edit1.text;
open;
showmessage(inttostr(recordCount));
...
看有没有记录

有个建议:你写的代码最好要规范,可以参照 ihihonline(小小->不要再流浪!!) 老兄
chenypluck 2002-08-31
  • 打赏
  • 举报
回复
朋友你好
谢谢你对stringgrid显示问题的指导:
我做了如下修改:

procedure form1.button1click()
var
i,j,k:integer;
begin
with query1 do
begin
close;
sql.clear;
sql.add('select * from biao');
sql.add('where 编号=:mid');
parambyname('mid').asstring:=edit1.text;
open;
stringgrid1.rowcount:=query1.recordcount;
stringgrid1.colcount:=query1.fieldcount;
for k:=0 to query1.fieldcount-1 do
begin
stringgrid1.cells[k,0]:=query1.fields[i].fieldname;//显示字段名
end;
if table1.findkey([edit1.text]) then //table1 是biao
begin
j:=1;
query1.first;
while not eof do
begin
for i:=1 to stringgrid1.colcount-1 do
stringgrid1.cells[i,j]:=query1.fields[i].asstring;
inc(j);
query1.next;
end;
end
else
showmessage('no find');
end;
end;
程序运行的问题是:
输入编号后,不能在stringgrid中显示出与编号相符合的记录,只能显示出字段名
sjunyi 2002-08-31
  • 打赏
  • 举报
回复
楼上两位综合起来试一下,应该可以了。
ihihonline 2002-08-31
  • 打赏
  • 举报
回复
procedure form1.button1click()
var
i,j:integer;
begin
with query1 do
begin
close;
sql.clear;
sql.add('select * from biao');
sql.add('where 编号=:mid');
parambyname('mid').asstring:=edit1.text;
open;
stringgrid1.rowcount:=query1.recordcount;
stringgrid1.colcount:=query1.fieldcount;
if table1.findkey([edit1.text]) then //table1 是biao
begin
j:=0;
while not eof do
begin
J ;=0;
for i:=0 to stringgrid1.colcount-1 do
begin
stringgrid1.cells[i,j]:=query1.fields[i].asstring;
inc(j);
end;
query1.next;
end;
end else
showmessage('no find');
end;
end;


Michaelyfj 2002-08-31
  • 打赏
  • 举报
回复
first;
//加上first,把query1的指针移到最前面
while not eof do begin
for i:=0 to stringgrid1.colcount-1 do
stringgrid1.cells[i,j]:=query1.fields[i].asstring;
inc(j);

5,930

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧