Solved

OnMouseDown/dragdrop for TDBGrid

Posted on 2001-09-10
3
574 Views
Last Modified: 2008-03-17
Hi, I'm trying to initiate drag & drop from a DBgrid but I want to do it manually so it does not interfere with normal cell editing/selection.  I normally use the OnMouseDown event to capture drag-drop but this only seems to get fired on the DBGrid for the row-headers!  Does anyone please know how can I get the desired drag-drop effect?  Thx.
0
Comment
Question by:Mangolab
3 Comments
 
LVL 27

Accepted Solution

by:
kretzschmar earned 100 total points
ID: 6471394
maybe this gives you a hint,

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
 
LVL 9

Expert Comment

by:ginsonic
ID: 6471509
listening
0
 

Author Comment

by:Mangolab
ID: 6472486
That's great thanks very much for your help.
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

Suggested Solutions

Title # Comments Views Activity
PDF library for Delphi 2 107
Virtuailstring tree compare node issue 14 105
HTML text in the body of an email (delphi code) 12 96
Need Help Delphi 2010 CheckBox1 Stored value in memo 13 57
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…
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…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

867 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

20 Experts available now in Live!

Get 1:1 Help Now