How to drag and click on DBGrid?

I need to write a program that can drag a row on DBGrid to another DBGrid, but i can not click on first DBGrid.

Give me some solutions.

Thanhks.
ngaltAsked:
Who is Participating?
 
kretzschmarConnect With a Mentor Commented:
hi ngalt,

a sample

unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    DBGrid1: TDBGrid;
    DBGrid2: TDBGrid;
    Table1: TTable;
    DataSource1: TDataSource;
    Table2: TTable;
    DataSource2: TDataSource;
    procedure DBGrid1MouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure DBGrid1MouseMove(Sender: TObject; Shift: TShiftState; X,
      Y: Integer);
    procedure DBGrid2DragOver(Sender, Source: TObject; X, Y: Integer;
      State: TDragState; var Accept: Boolean);
    procedure DBGrid2DragDrop(Sender, Source: TObject; X, Y: Integer);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

var DragX, DragY : Integer;

procedure TForm1.DBGrid1MouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  if Button = mbleft then  //Left Mousebutton
  Begin
    DragX := X;            //Remember Position
    DragY := Y;
  end;
end;

procedure TForm1.DBGrid1MouseMove(Sender: TObject; Shift: TShiftState; X,
  Y: Integer);
begin
  if (ssLeft in shift) then                 //Left Mousebutton
    if dbgrid1.SelectedRows.Count > 0 then  //Are there rows Selected
      If (abs(x-dragx) > 10) or             //10 Pixels moved?
         (abs(y-dragy) > 10) Then
        DBgrid1.BeginDrag(False,10);        //BeginDrag in additional 10 Pixelmove
end;

procedure TForm1.DBGrid2DragOver(Sender, Source: TObject; X, Y: Integer;
  State: TDragState; var Accept: Boolean);
begin
  Accept := source = dbgrid1;  //accept only dbgrid1
end;

procedure TForm1.DBGrid2DragDrop(Sender, Source: TObject; X, Y: Integer);
var i,j : Integer;
begin
  For I := 0 to dbgrid1.SelectedRows.Count - 1 do   //do move all selected records to other table
  begin
    DbGrid1.DataSource.DataSet.GotoBookmark(TBookMark(dbgrid1.SelectedRows[i]));
    DbGrid2.DataSource.DataSet.Append;
    for J := 0 to DbGrid1.DataSource.DataSet.FieldCount - 1 do
      DbGrid2.DataSource.DataSet.Fields[j].Value := DbGrid1.DataSource.DataSet.Fields[j].Value;
    DbGrid2.DataSource.DataSet.Post;
  end;
  dbgrid1.SelectedRows.Delete;   //Delete source records
end;

end.


meikl
0
 
ITugayCommented:
listening...
0
 
ITugayCommented:
Ok, usually, to start drag, you need to call BeginDrag method of component or set DragMode to dmAutomatic. Setting TDBGrid.DragMode = dmAutomatic  will force drag always you just click mouse in row. You can't select row by clicking left mouse button while DragMode=dmAutomatic, is it? The first solution is to start drag manually, by detecting MouseDown then MouseMove while mouse button is pressed. But TDBGrid  don't call onMoseDown when you click on it row with data. Therefore you need to write you own component from TDBGrid. If you need a sample, just let me know.

Igor.
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
ITugayCommented:
to kretzschmar.

How it possible to do something onMouseDown in TDBGrid when you click row with data? In my Delphi (D5), onMouseDown called only when I click to TDBGrid empty area. I miss something?

Best regards,
Igor.
0
 
kretzschmarCommented:
hi igor,

just checked this, and you're right, therefore some changes
- onmousedown is unusual just delete this procedure
- changes onmousemove:
procedure TForm1.DBGrid1MouseMove(Sender: TObject; Shift: TShiftState; X,
  Y: Integer);
begin
  if (ssLeft in shift) then                 //Left Mousebutton
    if dbgrid1.SelectedRows.Count > 0 then  //Are there rows Selected
        DBgrid1.BeginDrag(False,10);        //BeginDrag in additional 10 Pixelmove
end;

meikl
0
 
ITugayCommented:
hi meikl,

>>>if dbgrid1.SelectedRows.Count > 0 then  //Are there rows Selected
may I delete it too?;)

Cheers,
Igor.

0
 
kretzschmarCommented:
if you want? sure!

well, this sample is desgined to select more rows than one by using the ctrl-key and dragdropping the selected in one step.

meikl
0
 
ITugayCommented:
to kretzschmar,

I do not know about the owner of a question, but the solution is usefull to me.

Best regards,
Igor.


0
 
kretzschmarCommented:
nice to hear, igor ;-)
0
 
ngaltAuthor Commented:
Let me check meikl's exmaple.
0
 
IndefreiCommented:
Hi, Igor,

How about offering some pts to meikl for his help  ?!!
just post a q with some points for him

regards
Indi
0
 
ITugayCommented:
to indefrei,
why not?
will done;)


Igor.
0
 
kretzschmarCommented:
indi,
your interventions are sometimes surprising.
how are you ?

igor,
i collect it
thanks

ngalt,
any results?
something unclear?
additional q's about this ?

meikl
0
 
kretzschmarCommented:
hello ngalt,
are you still alive?
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.