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!
ongchu001Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Emmanuel PASQUIERFreelance Project ManagerCommented:
With StringGrid2 do
 begin
  RowCount:=RowCount+1;
  Cells[0,RowCount-1]:='New Text';
 end;
Emmanuel PASQUIERFreelance Project ManagerCommented:
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

ongchu001Author Commented:
thanks epasquier very much!
Exploring ASP.NET Core: Fundamentals

Learn to build web apps and services, IoT apps, and mobile backends by covering the fundamentals of ASP.NET Core and  exploring the core foundations for app libraries.

ongchu001Author Commented:
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?
Emmanuel PASQUIERFreelance Project ManagerCommented:
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

Emmanuel PASQUIERFreelance Project ManagerCommented:
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

ongchu001Author Commented:
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!
Emmanuel PASQUIERFreelance Project ManagerCommented:
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 );
ongchu001Author Commented:
thanks!well done!G9 epasquier!
Emmanuel PASQUIERFreelance Project ManagerCommented:
Hello ongchu,

don't forget to close your questions ! You have 3 that only wait your validation

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.