我在代码中有个StringGrid,其中StirngGrid2在前面已经对它负了值str :=StirngGrid2.cells[1,1] ;
showmessage(''+str+'');   //在此处能看到StirngGrid2.cells[1,1] 的内容
for i:=1 to 8 do  
   begin
     for j:=1 to 8 do 
       begin
         showmessage(''+str+''); //在这里就开始看不到 StirngGrid2.cells//[1,1] 的内容了,导致了没能把 StirngGrid2.cells[j,i] 的内容负给了相应的StirngGrid1         StirngGrid1.cells[j,i] := StirngGrid2.cells[j,i] ;
       end;有哪位知道这是怎么回事啊,或是怎么解决呢?
   end;

解决方案 »

  1.   

    是你的代码中对Str的变量重新赋值了吧?贴出的代码不全
      

  2.   

    完整代码如下:
    procedure DatasetToGrid(var MyGrid1,MyGrid2:TStringGrid;SqlString:string);
    var
      i,j,GridRow,GridCol,rowcount,colcount:integer;
      QueryCenter:TCenterQuery;
      Str : string ;
    begin
      QueryCenter:=TCenterQuery.Create(nil);
      MyGrid2.RowCount:=1;
      try
        with QueryCenter do
        begin
          close;
          sql.Clear;
          sql.add(sqlstring);
          open;
          if eof then exit;
          rowcount:=RecordCount;
          colcount:=FieldCount;
          MyGrid2.ColCount:=colcount + 1;
          MyGrid2.RowCount:=rowcount + 1;
          First;
          for i:=1 to rowcount  do
            begin
              for j:=1 to colcount do
                begin
                  MyGrid2.Cells[j,i]:=Fields[j - 1].AsString;
                end;
              Next;
            end;    end;
      finally
        QueryCenter.free;
      end;  Str := MyGrid2.Rows[1];
      showmessage(''+Str+'');
      For GridRow := 1 to i-1 do
        begin
          For GridCol := 1 to j-1 do
            begin
               Str := MyGrid2.Cells[j,i];
               showmessage(''+Str+'');
               MyGrid1.Cells[j,i]:= MyGrid2.Cells[j,i] ;
            end;
        end; end;
    在最下面的FOR循环前showmessage(''+Str+'');出来的就有内容在 FOR循环中的showmessage(''+Str+'');出来的就是空的了所以导致了MyGrid1.Cells[j,i]得到了个空值,这是怎么回事呢??