Solved

How to drag and click on DBGrid?

Posted on 2000-03-12
14
389 Views
Last Modified: 2010-04-04
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.
0
Comment
Question by:ngalt
14 Comments
 
LVL 9

Expert Comment

by:ITugay
ID: 2611203
listening...
0
 
LVL 9

Expert Comment

by:ITugay
ID: 2611227
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
 
LVL 27

Accepted Solution

by:
kretzschmar earned 150 total points
ID: 2611569
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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
LVL 9

Expert Comment

by:ITugay
ID: 2611611
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
 
LVL 27

Expert Comment

by:kretzschmar
ID: 2611631
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
 
LVL 9

Expert Comment

by:ITugay
ID: 2611670
hi meikl,

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

Cheers,
Igor.

0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 2611685
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
 
LVL 9

Expert Comment

by:ITugay
ID: 2611707
to kretzschmar,

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

Best regards,
Igor.


0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 2611729
nice to hear, igor ;-)
0
 

Author Comment

by:ngalt
ID: 2614804
Let me check meikl's exmaple.
0
 
LVL 1

Expert Comment

by:Indefrei
ID: 2617870
Hi, Igor,

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

regards
Indi
0
 
LVL 9

Expert Comment

by:ITugay
ID: 2618875
to indefrei,
why not?
will done;)


Igor.
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 2620976
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
 
LVL 27

Expert Comment

by:kretzschmar
ID: 2639678
hello ngalt,
are you still alive?
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Delphi: how to send PJL commands to printer 3 97
HTML text in the body of an email (delphi code) 12 113
oracle global variables 4 68
DBCtrlGrid, Delphi, Scroll 8 8
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
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…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.

786 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