4th february

Pascal

Delphi

VCL Components

CLX Components

Glyphs, Icons, Cursors, Videos

Logos

Sample Projects

Tools

Articles, Code Examples, Tips

Links

Code Examples

How to move rows in StringGrid?

Put the TStringGrid component on the form. Set value 2 for property ColCount. Put the TButton component on the form and create procedure OnClick for TButton. Type the following code:

{ Move row up }
procedure TForm1.Button1Click(Sender: TObject);
var
  PrevCol1, PrevCol2: string;
begin
  if StringGrid1.Row = 1 then Exit;
  with StringGrid1 do
  begin
  { Saving cell values in previous row }
    PrevCol1 := Cells[0, Row-1];
    PrevCol2 := Cells[1, Row-1];
  { Changing cell values in previous row }
    Cells[0, Row-1] := Cells[0, Row];
    Cells[1, Row-1] := Cells[1, Row];
  { Saving cell values in curreent row }
    Cells[0, Row] := PrevCol1;
    Cells[1, Row] := PrevCol2;
  { Moving focus selection }
    Row := Row - 1;
    SetFocus;
  end;
end;