Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How to drag and click on DBGrid?

Posted on 2000-03-12
14
Medium Priority
?
417 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 450 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
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…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Suggested Courses

610 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