Delphi中怎样比较大小?

chuanmail 2009-07-16 08:32:12
我在form中设置了2个stringgrid。我想把第一个stringgrid的数据有条件的导入到第二个stringgrid中,条件是大于某一列的值,运行时,前三列有数据,从这之后stringgrid中就没数据了,代码如下,请大家帮个忙,解决一下。

procedure TForm1.Button5Click(Sender: TObject);
var
i,j,ccount:integer;
begin
ccount:=StringGrid1.ColCount;
for i:=1 to StringGrid1.RowCount do
begin
stringgrid2.Cells[1,i]:=stringgrid1.Cells[1,i];
stringgrid2.Cells[2,i]:=stringgrid1.Cells[2,i];
stringgrid2.Cells[3,i]:=stringgrid1.Cells[3,i];

for j:=4 to ccount-10 do //数据比较多
begin
if stringgrid1.Cells[j,i]>stringgrid1.Cells[ccount-3,i] then //每一个值和倒数第三列的值比较,谁小,取谁
stringgrid2.Cells[j,i]:=stringgrid1.Cells[ccount-3,i] else
stringgrid2.Cells[j,i]:=stringgrid1.Cells[j,i];
end;
end;
showmessage('数据替换成功!')
end;
...全文
387 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
sg_knight 2009-07-17
  • 打赏
  • 举报
回复
学习。
xinxinshou2009 2009-07-16
  • 打赏
  • 举报
回复
那是因为你的数据中有空值,你可以先判断数值是否为空,然后再用我的方法比较

for j:=4 to ccount-10 do
begin
if stringgrid1.Cells[j,i]<>'' then
begin
if strtofloat(stringgrid1.Cells[j,i])>strtofloat(stringgrid1.Cells[ccount-3,i]) then
stringgrid2.Cells[j,i]:=stringgrid1.Cells[ccount-3,i] else
stringgrid2.Cells[j,i]:=stringgrid1.Cells[j,i];
end else
begin
......//这儿写数据为空时的操作
end;
chuanmail 2009-07-16
  • 打赏
  • 举报
回复
回复五楼新新手,这个方法我也试过,但在数据替换是出现错误,错误提示如下‘’ is not a valid floating point value
xinxinshou2009 2009-07-16
  • 打赏
  • 举报
回复
就是我说的,你改一下就行
chuanmail 2009-07-16
  • 打赏
  • 举报
回复
谢谢各位的热心帮助,都怪我没把问题说清楚,在我的程序界面的第一个stringgrid中,每行的前三列都是字符,不需要比较。从第四列开始到倒数第10列,都是带小数的数据,我的目的是把这些数据和倒数第三列的数据比较,把其中较小的值写入stringgrid2中。
大家看一下这段代码就清楚了:
for j:=4 to ccount-10 do //数据比较多
begin
if stringgrid1.Cells[j,i]>stringgrid1.Cells[ccount-3,i] then
stringgrid2.Cells[j,i]:=stringgrid1.Cells[ccount-3,i] else
stringgrid2.Cells[j,i]:=stringgrid1.Cells[j,i];
end;
bdmh 2009-07-16
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 sonicer 的回复:]
字串比较大小和数值比较大小稍有区别,可以在一些相关的书籍上看看学习一下
[/Quote]
字符串和数字当然不是一回事,你可以把字符转为数字去比较,StrToInt('字符串')
sanguomi 2009-07-16
  • 打赏
  • 举报
回复
你得转换下再比较
林石公 2009-07-16
  • 打赏
  • 举报
回复
字串比较大小和数值比较大小稍有区别,可以在一些相关的书籍上看看学习一下
bdmh 2009-07-16
  • 打赏
  • 举报
回复
你到底想说明什么问题,你遇到的是什么问题
xjwy 2009-07-16
  • 打赏
  • 举报
回复
我还是觉得你没说清楚
xinxinshou2009 2009-07-16
  • 打赏
  • 举报
回复
if stringgrid1.Cells[j,i]>stringgrid1.Cells[ccount-3,i] then //每一个值和倒数第三列的值比较,谁小,取谁

这句要改,如果你的stringgrid中的数据全部是整数,就改成
if strtoint(stringgrid1.Cells[j,i])>strtoint(stringgrid1.Cells[ccount-3,i]) then

如果表格中有小数,就改成
if strtofloat(stringgrid1.Cells[j,i])>strtofloat(stringgrid1.Cells[ccount-3,i]) then

16,748

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 语言基础/算法/系统设计
社区管理员
  • 语言基础/算法/系统设计社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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