Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 804
  • Last Modified:

DBGrid drag&drop

I am trying DBGrid drag&drop to change the seq (a ranking field) through adoQuery. I can get the destination seq, but I can't get the source seq (the code in StartDrag event doesn't seem to work)

Can anyone give a simple example using DBGrid drag&drop to get the SOURCE record data?

Thanks in advance.

//******Code as below******
procedure TtestForm.DBGrid1StartDrag(Sender: TObject;
  var DragObject: TDragObject);
var
  i:integer;
  s:string;
begin
  i:=qry1.fieldByName('seq').asinteger;
  s:=intToStr(i);

  edtStart.Clear;
  edtStart.Text:=s;
  edtStart.Text:=edtEnd.Text;
end;

procedure TtestForm.DBGrid1DragOver(Sender, Source: TObject; X, Y: Integer;
  State: TDragState; var Accept: Boolean);
begin
  Accept := Source is TDBGrid;
end;

procedure TtestForm.DBGrid1DragDrop(Sender, Source: TObject; X,
  Y: Integer);
var
  cl:TGridCoord;
  i,recNo1:integer;
  s:string;
begin
  cl:=DBGrid1.MouseCoord(x,y);
  recNo1:=cl.Y;
  with qry1 do
  begin
    DisableControls;
    first;
    if recNo1>1 then
    begin
      for i:=1 to recNo1-1 do
        next;
    end;
    EnableControls;

    s:=qry1.fieldbyname('seq').AsString;
    edtEnd.Clear;
    edtEnd.Text:=s;
  end;
end;
0
ourselves
Asked:
ourselves
1 Solution
 
kretzschmarCommented:
from my paq (even not with ado, but should work too)

a sample for resorting grid entries with drag&drop

unit dbg_dd_u;

interface

uses
 Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
 Grids, DBGrids, Db, DBTables;

type
 TForm1 = class(TForm)
   Table1: TTable;
   DataSource1: TDataSource;
   DBGrid1: TDBGrid;
   procedure DBGrid1MouseMove(Sender: TObject; Shift: TShiftState; X,
     Y: Integer);
   procedure DBGrid1DragOver(Sender, Source: TObject; X, Y: Integer;
     State: TDragState; var Accept: Boolean);
   procedure FormCreate(Sender: TObject);
   procedure DBGrid1MouseUp(Sender: TObject; Button: TMouseButton;
     Shift: TShiftState; X, Y: Integer);
   procedure DBGrid1DragDrop(Sender, Source: TObject; X, Y: Integer);
 private
   InDrag : Boolean;
   RowIndex : Integer;
   { Private declarations }
 public
   { Public declarations }
 end;

var
 Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.DBGrid1MouseMove(Sender: TObject; Shift: TShiftState; X,
 Y: Integer);
Var GridCoord : TGridCoord;
begin
 if (ssleft in shift) and not InDrag then
 begin
   GridCoord := DBgrid1.MouseCoord(x,y);
   if GridCoord.y > -1 then  //am i on a record
   begin
     RowIndex := GridCoord.y; //Remember the row
     DBGrid1.BeginDrag(False,5);  //begin in 5 additional pixelsmoves
     InDrag := True; //Now Dragging
   end;
 end;

end;

procedure TForm1.DBGrid1DragOver(Sender, Source: TObject; X, Y: Integer;
 State: TDragState; var Accept: Boolean);
Var GridCoord : TGridCoord;
begin
 GridCoord := DBgrid1.MouseCoord(x,y);
 Accept := (Sender = Source) and    //Accept only if the mouse
           (GridCoord.y > -1) and   //points to another record
           (GridCoord.y <> RowIndex)
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
 InDrag := False;  //Dragging done
end;

procedure TForm1.DBGrid1MouseUp(Sender: TObject; Button: TMouseButton;
 Shift: TShiftState; X, Y: Integer);
begin
 InDrag := False;  //Dragging done
end;

procedure TForm1.DBGrid1DragDrop(Sender, Source: TObject; X, Y: Integer);
Var
 GridCoord : TGridCoord;
 BMSource, BMDest : TBookmark;
 DestPos, SourcePos : Integer;
begin
 Table1.DisableControls;                   //Don't Show the action
 GridCoord := DBgrid1.MouseCoord(x,y);     //Where are we now
 BMSource := Table1.GetBookmark;           //Remember Source
 SourcePos := Table1.FieldByName('Pos').AsInteger;  //Remember SourcePosValue
 Table1.MoveBy(GridCoord.y-RowIndex);      //got to DestRecord
 DestPos := Table1.FieldByName('Pos').AsInteger;  //Remember DestPosValue
 Table1.Edit;                              //Avoiding keyviolation
 Table1.FieldByName('Pos').AsInteger := -1;//Park DestRecord
 Table1.Post;
 BMDest := Table1.GetBookmark;             //Remember DestRecord
 Table1.GotoBookmark(BMSource);            //Goto to SourceRecord
 Table1.Edit;                              //Update Pos From DestRecord
 Table1.FieldByName('Pos').AsInteger := DestPos;
 Table1.Post;
 Table1.GotoBookmark(BMDest);              //Goto to DestRecord
 Table1.Edit;                              //Update Pos From SourceRecord
 Table1.FieldByName('Pos').AsInteger := SourcePos;
 Table1.Post;
 Table1.FreeBookmark(BMSource);            //Forget the Rembering
 Table1.FreeBookmark(BMDest);
 Table1.EnableControls;                    //Show The Change
 InDrag := False;                          //Dragging done
end;


end.


meikl ;-)
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now