Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

dbGrid Drag & Drop

Status
Not open for further replies.

guava65

Programmer
Jul 20, 2001
238
US
I have 2 dbGrids each associated tables of the same structure. I would like to drag records from grid A to Grid B (either single or multiple).

I've looked aroung for examples but there doesn't seem to be any for the dbGrid.

Can someone help with the necessary steps/code or point me in the right direction?

ty

Aloha,
cg
 
1. If you are not familiar with drag and drop generally, see Delphi help for "Drag-and-drop"

2. To drag and drop between grids you need two extra properties on the TDBGrid, so you need to subclass it. (Pity Borland just don't include these properties in the out of the box version)

Here is the code. It has been copied from a Borland article as acknowledged.

******************************************
unit SHDBGridU;

interface

uses
DBGrids, Controls, Classes;

type

{Dragging and dropping copied from Borland article TI1562D dated 30th March 1999}

TSHDBGrid = class(TDBGrid)
private
FOnMouseDown: TMouseEvent; {To enable dragging and dropping}
protected
procedure MouseDown(Button: TMouseButton; Shift: TShiftState;
X, Y: Integer); override; {To enable dragging and dropping}
published
property Row; {To enable dragging and dropping}
property OnMouseDown read FOnMouseDown write FOnMouseDown; {To enable dragging and dropping}
end;

procedure Register;

implementation

procedure Register;
begin
RegisterComponents('SHLibrary', [TSHDBGrid]);
end;

procedure TSHDBGrid.MouseDown(Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
if Assigned(FOnMouseDown) then
FOnMouseDown(Self, Button, Shift, X, Y);
inherited MouseDown(Button, Shift, X, Y);
end;

end.
******************************************

3. OnMouseDown for the source grid

*******************************************
procedure TMyForm.SourceGridMouseDown(Sender: TObject;
Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
var
LCell:TGridCoord;
begin
inherited;

if Button = mbLeft then { drag only if left button pressed }
with SystemConnectGrid do begin
LCell:=MouseCoord(X,Y);

if(LCell.Y>=0)then begin
BeginDrag(False);
end;
end;
end;
************************************

4. OnDragOver for destination grid

************************************
procedure TMyForm.DestGridDragOver(Sender,
Source: TObject; X, Y: Integer; State: TDragState; var Accept: Boolean);
var
LCell:TGridCoord;
begin
inherited;
if(Source=SourceGrid)then begin
LCell:=(Sender as TSHDBGrid).MouseCoord(X,Y);
if(LCell.Y>=0)then begin
{Move current record to one under the mouse}
(Sender as TSHDBGrid).DataSource.Dataset.MoveBy(LCell.Y-(Sender as TSHDBGrid).Row);
Accept:=true;
end;
end;
end;
************************************

5. OnDragDrop for destination grid

************************************
procedure TMyForm.DestGridDragDrop(Sender,
Source: TObject; X, Y: Integer);
var
LCell:TGridCoord;
begin
inherited;

if(Source=SourceGrid)then begin
LCell:=(Sender as TSHDBGrid).MouseCoord(X,Y);
if(LCell.Y>=0)then begin
with (Sender as TSHDBGrid).Datasource.DataSet do begin
{Move current record to one under the mouse.
Should already be there}
MoveBy(LCell.Y-(Sender as TSHDBGrid).Row);

{Whatever update code your drag and drop operation requires. For example:}
Edit;
FieldByName('FieldName').AsInteger:=SourceGrid.Datasource.DataSet.FieldByName('FieldName').AsInteger;
Post;

end;
end;
end;
end;
*****************************************

Have fun with it
Simon
 
Geeez,

That is much more than I had anticipated just to get setup. I'm glad I finaly asked the question.

Thanks a lot.


Aloha,
cg
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top