我在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;

解决方案 »

  1.   

    字符串和数字当然不是一回事,你可以把字符转为数字去比较,StrToInt('字符串')
      

  2.   

     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
      

  3.   

    谢谢各位的热心帮助,都怪我没把问题说清楚,在我的程序界面的第一个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; 
      

  4.   

    回复五楼新新手,这个方法我也试过,但在数据替换是出现错误,错误提示如下‘’ is not a valid floating point value
      

  5.   

    那是因为你的数据中有空值,你可以先判断数值是否为空,然后再用我的方法比较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;