其中stringgrid第三列为数字,比较第三列数值排序,可是程序运行是出现系统错误,这是那里出现问题了?var
row1,row2,TempRow:integer;
Skip1,Skip2:integer;
TempCol1,TempCol2,TempCol3:string;
begin
TempRow:=0;
for row1:=RowCount-2 DownTo TempRow  do
 begin
  TempRow:=TempRow+1;
  for row2:=row1 DownTo TempRow do
   begin
   Skip1:=strtoint(Form2.StringGrid1.Cells[2,row2+1]);
   Skip2:=strtoint(Form2.StringGrid1.Cells[2,row2]);
   if Skip1>Skip2 then
    begin
    TempCol1:=Form2.StringGrid1.Cells[0,row2];
    Form2.StringGrid1.Cells[0,row2]:=Form2.StringGrid1.Cells[0,row2+1];
    Form2.StringGrid1.Cells[0,row2+1]:=TempCol1;
    TempCol2:=Form2.StringGrid1.Cells[1,row2];
    Form2.StringGrid1.Cells[1,row2]:=Form2.StringGrid1.Cells[1,row2+1];
    Form2.StringGrid1.Cells[1,row2+1]:=TempCol2;
    TempCol2:=Form2.StringGrid1.Cells[2,row2];
    Form2.StringGrid1.Cells[2,row2]:=Form2.StringGrid1.Cells[2,row2+1];
    Form2.StringGrid1.Cells[2,row2+1]:=TempCol3;
    Form2.StringGrid1.Update;
    end;
   end;
 end;
end;

解决方案 »

  1.   

    至少我看的你for循环中的变量TempRow在循环内部还在变,这样很容易越界或非法访问,你换成另一个变量试试吧
      

  2.   


    var
      i,j,vi,vj: integer;
    begin
      for i:=0 to RowCount-2 do begin
        for j:=i To RowCount-1 do begin
          vi:=strtoint(Form2.StringGrid1.Cells[2,i]);
          vj:=strtoint(Form2.StringGrid1.Cells[2,j]);
          if vi>vj then begin
            //交换两行数据
          end;
        end;
      end;
    end;