unit Unit1;interfaceuses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, Grids, StdCtrls;type
  TForm1 = class(TForm)
    Button1: TButton;
    StringGrid1: TStringGrid;
    StringGrid2: TStringGrid;
    Button2: TButton;
    procedure Button1Click(Sender: TObject);
    procedure StringGrid1DblClick(Sender: TObject);
    procedure StringGrid2DblClick(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;var
  Form1: TForm1;implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
begin
  with self.StringGrid1 do
  begin
    row:=0;
    repeat
      rows[row].Clear;
      if row<rowcount-1 then 
        row:=row+1;
    until row=rowcount-1;
    repeat
      cells[0,row]:=inttostr(row);
      rowcount:=rowcount+1;
      row:=row+1;
    until row=10 ;
    showmessage(inttostr(row)+','+inttostr(rowcount));
  end;    // with
   
end;procedure TForm1.StringGrid1DblClick(Sender: TObject);
begin
  with StringGrid1 do
  begin
    if cells[0,row]<>''then
    begin
      StringGrid2.Rows[stringgrid2.rowcount-1].AddStrings(rows[row]);
      StringGrid2.RowCount:=StringGrid2.RowCount+1;
    end;         
    if row=rowcount-1 then
    begin
      rows[row].Clear;
      rowcount:=rowcount-1;
      exit;
    end;  
    repeat
      rows[row]:=rows[row+1];
      if row<rowcount-1 then
        row:=row+1;
    until row=rowcount-1;
    rowcount:=rowcount-1;
    showmessage(inttostr(row)+','+inttostr(rowcount));
  end;    // with
  
end;procedure TForm1.StringGrid2DblClick(Sender: TObject);
begin
  with StringGrid2 do
  begin
    if cells[0,row]<>'' then
    begin
      stringGrid1.Rows[stringgrid1.RowCount-1].AddStrings(rows[row]);
      StringGrid1.RowCount:=StringGrid1.RowCount+1;
    end;  
    if row=rowcount-1 then
    begin
      rowcount:=rowcount-1;
      exit;
    end;
    repeat
      rows[row].Clear;
      rows[row]:=rows[row+1];
      if row<rowcount-1  then
        row:=row+1;        
    until row=rowcount-1;
    rowcount:=rowcount-1;
    showmessage(inttostr(row)+','+inttostr(rowcount));
  end;    // with
 
end;{procedure TForm1.Button2Click(Sender: TObject);
begin
   with StringGrid1 do
   begin
     rows[row].Delete(row);
   end;    // with
end;
 }
end.
这段代码编译正常,运行后,点多几次就出错了, OUT OF LISTBOUND(-1),为什么?

解决方案 »

  1.   

    代码的说明:
    窗体上放2个STRINGGRID,给其中一个先填点东西,然后双击任一行,将其添入另一个,并且删除点中的那行;如果是空行,不添加,只删除空行;
    实际上是间接实现拖拽功能
      

  2.   

    错误出在这里:StringGrid1.RowCount:=StringGrid1.RowCount+1;
    这样写肯定会出错,用一个变量来传递就不会出错了。
      

  3.   

    不好意思:问题在stringGrid1.Rows[stringgrid1.RowCount-1].AddStrings(rows[row])这行。
      

  4.   

    前几天刚做的,看看慢不满足
    var I,k,m:Integer;
    begin
    if AlarmList.Row >0 then
    begin
    with AlarmList do
    begin
    for k := row to RowCount  do
    begin
      if Cells[0, K] = '√' then
      begin
       begin
        REOper.Lines.Add(FormatDateTime('YYYY年MM月DD日 HH:MM:SS',Now));
        REOper.Lines.Add(Rows[K].CommaText);
        AlarmList.Rows[K].Clear;
        AlarmList.Rows[K].Assign(Rows[K+1]);
    //    Rows[k]:=Rows[k+1] ;
    //    MoveColumn(Row, RowCount -1);
        RowCount := RowCount - 1;
        Refresh;
     end;
    end;
    end;
    end;
    end;
      

  5.   

    to 
    : Danphel(Aleon,看不懂啊
      

  6.   

    AlarmList.Rows[K].Assign(Rows[K+1]);
    问题解决了,就是用这个ASSIGN方法就好了.谢谢了.
    不过究竟是什么原因呐?VCL内部的问题吗?Danphel(Aleon老大,能不能详细说明一下.
      

  7.   

    ASSIGN在此的用法是这样的,先把本行给清空,把下一行赋值给本行,其实使用覆盖也是可以的。
      AlarmList.Rows[K].Clear;
      AlarmList.Rows[K].Assign(Rows[K+1]);
    这是一个函数内的一段,没有贴全,见谅