Solved

OnMouseDown/dragdrop for TDBGrid

Posted on 2001-09-10
3
570 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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

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…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

747 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

10 Experts available now in Live!

Get 1:1 Help Now