Solved

DBGrid drag&drop

Posted on 2002-06-09
1
724 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

863 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now