Here's a small example. Start a new project and call the main form mainTest. Adapt the code as follows:
unit mainTest;
{$mode objfpc}{$H+}
interface
uses
SysUtils, Forms, Controls, GridDragDrop;
type
{ TForm1 }
TForm1 = class(TForm)
procedure FormCreate(Sender: TObject);
private
sg: TDragDropRows;
end;
var
Form1: TForm1;
implementation
{$R *.lfm}
{ TForm1 }
procedure TForm1.FormCreate(Sender: TObject);
var
r,c: integer;
begin
sg:=TDragDropRows.Create(Self);
sg.Align:=alClient;
sg.ColCount:=5;
for r:= 0 to sg.RowCount-1 do
for c:= 0 to sg.ColCount-1 do
sg.Cells[c,r]:=Format('row:%d',[r]);
sg.Parent:=Self;
// set other properties here ...
end;
end.
Then create a new unit called GridDragDrop, add it to the project and add this code:
unit GridDragDrop;
{$mode objfpc}{$H+}
interface
uses
Classes, Grids, Controls;
type
{ TDragDropGrid }
TDragDropRows = class(TCustomStringGrid)
private
function RowAtXY(anX, aY: integer): integer;
procedure GridDragOver(Sender, Source: TObject;
X,Y: Integer; State: TDragState; var Accept: Boolean);
procedure GridDragDrop(Sender, Source: TObject; X,Y: Integer);
public
constructor Create(AOwner: TComponent); override;
end;
implementation
{ TDragDropGrid }
function TDragDropRows.RowAtXY(anX, aY: integer): integer;
var
c: integer;
begin
MouseToCell(anX, aY, c, Result);
end;
procedure TDragDropRows.GridDragOver(Sender, Source: TObject; X, Y: Integer;
State: TDragState; var Accept: Boolean);
var sourceGrid: TStringGrid;
begin
if (Sender<>Source) or not (Source is TDragDropRows) then Exit;
sourceGrid:=TStringGrid(Source);
Accept:=sourceGrid.Row<>RowAtXY(X, Y);
end;
procedure TDragDropRows.GridDragDrop(Sender, Source: TObject; X, Y: Integer);
begin
MoveColRow(False,(Source as TDragDropRows).Row, RowAtXY(X, Y));
end;
constructor TDragDropRows.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
DragMode:=dmAutomatic;
OnDragOver:=@GridDragOver;
OnDragDrop:=@GridDragDrop;
// set other properties such as Options here
end;
end.