Solved

How to drag and click on DBGrid?

Posted on 2000-03-12
14
386 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
 
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
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.

 
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

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

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
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…
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.
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…

911 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