Solved

DBGrid drag&drop

Posted on 2002-06-09
1
737 Views
Last Modified: 2008-02-01
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
Comment
Question by:ourselves
1 Comment
 
LVL 27

Accepted Solution

by:
kretzschmar earned 50 total points
ID: 7066226
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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
find a node in VST 2 81
Microsoft Access 97 and Delphi XE2 9 74
Problem working with dynamic array - help 2 36
Firemonkey how to swip panel with finger tap 1 25
A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

696 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question