Link to home
Start Free TrialLog in
Avatar of ongchu001
ongchu001

asked on

copy test in row of stringgrid.

hello everybody.
i'm working with stringgrid.i have a question.
if i want to copy text in the row of stringgrid1, to the  empty row of stringgrid2?How can i do?if stringgrid2  does't have any  empty row --> stringgrid2 automatic insert new row and copy text from stringgrid1.
thanks a lot!
Avatar of Emmanuel PASQUIER
Emmanuel PASQUIER
Flag of France image

With StringGrid2 do
 begin
  RowCount:=RowCount+1;
  Cells[0,RowCount-1]:='New Text';
 end;
if you want a complete copy of a row from StringGrid1 in a new row in StringGrid2 (assuming they have the same number of column, you do that :

CopyStringGridRow(StringGrid1,StringGrid2);
// will use default parameter for SGFromRow & SGToRow => currently selected cell row for source, new row for destination

Or if you want to copy the currently selected row in source to the currently selected row in destination :
CopyRow(StringGrid1,StringGrid2,-1, StringGrid2.Row);
Procedure CopyStringGridRow(SGFrom,SGTo:StringGrid;SGFromRow:Integer=-1;SGToRow:Integer=-1);
var
 c:integer;
begin
 if SGFromRow=-1 Then SGFromRow:=SGFrom.Row;
 if SGToRow=-1 Then With SGTo do
  begin
   SGToRow:=RowCount;
   RowCount:=RowCount+1;
  end;
 for c:=0 to SGFrom.ColCount-1 do
  SGTo.Cells[c,SGToRow]:=SGFrom.Cells[c,SGFromRow];
end;

Open in new window

Avatar of ongchu001
ongchu001

ASKER

thanks epasquier very much!
epasquier?what do u think if i want to copy test in cell rows stringgrid1 to the first empty test cell rows stringgrid2 ,this is automatic and not have to choose the cell rows of stringgrid2?
assuming you have such empty rows ?
function EmptyRow(SG:TStringGrid;aRow:integer):Boolean;
var i:integer;
begin
 Result:=False;
 for i:=0 to SG.ColCount-1 do if SG.Cells[i,aRow]<>'' Then Exit;
 Result:=True;
end;

Procedure CopyStringGridRow(SGFrom,SGTo:StringGrid;SGFromRow:Integer=-1;SGToRow:Integer=-2);
var
 c:integer;
begin
 if SGFromRow=-1 Then SGFromRow:=SGFrom.Row;
 if SGToRow=-2 Then
  begin
   for SGToRow:=SGTo.RowCount-1 downto 0 do if EmptyRow(SGTo,SGToRow) 
    Then break;
   // if no empty line found, SGToRow should be = -1 => create a new one
  end;
 if SGToRow=-1 Then With SGTo do
  begin
   SGToRow:=RowCount;
   RowCount:=RowCount+1;
  end;
 for c:=0 to SGFrom.ColCount-1 do
  SGTo.Cells[c,SGToRow]:=SGFrom.Cells[c,SGFromRow];
end;

Open in new window

little pb : it will fill from the end of the grid. To fill from the first empty rows :
Procedure CopyStringGridRow(SGFrom,SGTo:StringGrid;SGFromRow:Integer=-1;SGToRow:Integer=-2);
var
 c:integer;
begin
 if SGFromRow=-1 Then SGFromRow:=SGFrom.Row;
 if SGToRow=-2 Then
  begin
   for SGToRow:=0 to SGTo.RowCount-1 do if EmptyRow(SGTo,SGToRow) 
    Then break;
   if SGToRow=SGTo.RowCount Then SGToRow:=-1;
  end;
 if SGToRow=-1 Then With SGTo do
  begin
   SGToRow:=RowCount;
   RowCount:=RowCount+1;
  end;
 for c:=0 to SGFrom.ColCount-1 do
  SGTo.Cells[c,SGToRow]:=SGFrom.Cells[c,SGFromRow];
end;

Open in new window

Hi epasquier!Can u tell me about this problem.
(1).When i do that:
CopyStringGridRow(stringgrid1,stringgrid2);
This test copy in to the last empty cell row of StringGrid2.Then Copy in to the above cell row empty of Stringgrid2.Then Copy to above Fixed cell row Stringgrid2(that i don't want).I want to copy this test to above empty cell row of StringGrid2 to below empty cell row (If no  empty-->creat one)

(2).If in stringgrid1 i don't have to choose cell row,but i want to copy rows,which  rows have fixed cell rows with text ''PK"  in to empty cells rows of Stringgrid2  as (1)
Thank u much for the help!
1) use the second version of the procedure to fix this problem

2)

With StringGrid1 do for i:=0 to RowCount-1 do if Cells[0,i]='PK' Then CopyStringGridRow(StringGrid1 , StringGrid2 , i );
thanks!well done!G9 epasquier!
ASKER CERTIFIED SOLUTION
Avatar of Emmanuel PASQUIER
Emmanuel PASQUIER
Flag of France image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial