Solved

How to drag and click on DBGrid?

Posted on 2000-03-12
14
383 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
Comment Utility
listening...
0
 
LVL 9

Expert Comment

by:ITugay
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 9

Expert Comment

by:ITugay
Comment Utility
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
Comment Utility
nice to hear, igor ;-)
0
 

Author Comment

by:ngalt
Comment Utility
Let me check meikl's exmaple.
0
 
LVL 1

Expert Comment

by:Indefrei
Comment Utility
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
Comment Utility
to indefrei,
why not?
will done;)


Igor.
0
 
LVL 27

Expert Comment

by:kretzschmar
Comment Utility
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
Comment Utility
hello ngalt,
are you still alive?
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

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…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
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, fr…
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…

772 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

11 Experts available now in Live!

Get 1:1 Help Now