Solved

DBGrid drag&drop

Posted on 2002-06-09
1
715 Views
Last Modified: 2008-02-01
I am trying DBGrid drag&drop to change the seq (a ranking field) through adoQuery. I can get the destination seq, but I can't get the source seq (the code in StartDrag event doesn't seem to work)

Can anyone give a simple example using DBGrid drag&drop to get the SOURCE record data?

Thanks in advance.

//******Code as below******
procedure TtestForm.DBGrid1StartDrag(Sender: TObject;
  var DragObject: TDragObject);
var
  i:integer;
  s:string;
begin
  i:=qry1.fieldByName('seq').asinteger;
  s:=intToStr(i);

  edtStart.Clear;
  edtStart.Text:=s;
  edtStart.Text:=edtEnd.Text;
end;

procedure TtestForm.DBGrid1DragOver(Sender, Source: TObject; X, Y: Integer;
  State: TDragState; var Accept: Boolean);
begin
  Accept := Source is TDBGrid;
end;

procedure TtestForm.DBGrid1DragDrop(Sender, Source: TObject; X,
  Y: Integer);
var
  cl:TGridCoord;
  i,recNo1:integer;
  s:string;
begin
  cl:=DBGrid1.MouseCoord(x,y);
  recNo1:=cl.Y;
  with qry1 do
  begin
    DisableControls;
    first;
    if recNo1>1 then
    begin
      for i:=1 to recNo1-1 do
        next;
    end;
    EnableControls;

    s:=qry1.fieldbyname('seq').AsString;
    edtEnd.Clear;
    edtEnd.Text:=s;
  end;
end;
0
Comment
Question by:ourselves
1 Comment
 
LVL 27

Accepted Solution

by:
kretzschmar earned 50 total points
Comment Utility
from my paq (even not with ado, but should work too)

a sample for resorting grid entries with drag&drop

unit dbg_dd_u;

interface

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

type
 TForm1 = class(TForm)
   Table1: TTable;
   DataSource1: TDataSource;
   DBGrid1: TDBGrid;
   procedure DBGrid1MouseMove(Sender: TObject; Shift: TShiftState; X,
     Y: Integer);
   procedure DBGrid1DragOver(Sender, Source: TObject; X, Y: Integer;
     State: TDragState; var Accept: Boolean);
   procedure FormCreate(Sender: TObject);
   procedure DBGrid1MouseUp(Sender: TObject; Button: TMouseButton;
     Shift: TShiftState; X, Y: Integer);
   procedure DBGrid1DragDrop(Sender, Source: TObject; X, Y: Integer);
 private
   InDrag : Boolean;
   RowIndex : Integer;
   { Private declarations }
 public
   { Public declarations }
 end;

var
 Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.DBGrid1MouseMove(Sender: TObject; Shift: TShiftState; X,
 Y: Integer);
Var GridCoord : TGridCoord;
begin
 if (ssleft in shift) and not InDrag then
 begin
   GridCoord := DBgrid1.MouseCoord(x,y);
   if GridCoord.y > -1 then  //am i on a record
   begin
     RowIndex := GridCoord.y; //Remember the row
     DBGrid1.BeginDrag(False,5);  //begin in 5 additional pixelsmoves
     InDrag := True; //Now Dragging
   end;
 end;

end;

procedure TForm1.DBGrid1DragOver(Sender, Source: TObject; X, Y: Integer;
 State: TDragState; var Accept: Boolean);
Var GridCoord : TGridCoord;
begin
 GridCoord := DBgrid1.MouseCoord(x,y);
 Accept := (Sender = Source) and    //Accept only if the mouse
           (GridCoord.y > -1) and   //points to another record
           (GridCoord.y <> RowIndex)
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
 InDrag := False;  //Dragging done
end;

procedure TForm1.DBGrid1MouseUp(Sender: TObject; Button: TMouseButton;
 Shift: TShiftState; X, Y: Integer);
begin
 InDrag := False;  //Dragging done
end;

procedure TForm1.DBGrid1DragDrop(Sender, Source: TObject; X, Y: Integer);
Var
 GridCoord : TGridCoord;
 BMSource, BMDest : TBookmark;
 DestPos, SourcePos : Integer;
begin
 Table1.DisableControls;                   //Don't Show the action
 GridCoord := DBgrid1.MouseCoord(x,y);     //Where are we now
 BMSource := Table1.GetBookmark;           //Remember Source
 SourcePos := Table1.FieldByName('Pos').AsInteger;  //Remember SourcePosValue
 Table1.MoveBy(GridCoord.y-RowIndex);      //got to DestRecord
 DestPos := Table1.FieldByName('Pos').AsInteger;  //Remember DestPosValue
 Table1.Edit;                              //Avoiding keyviolation
 Table1.FieldByName('Pos').AsInteger := -1;//Park DestRecord
 Table1.Post;
 BMDest := Table1.GetBookmark;             //Remember DestRecord
 Table1.GotoBookmark(BMSource);            //Goto to SourceRecord
 Table1.Edit;                              //Update Pos From DestRecord
 Table1.FieldByName('Pos').AsInteger := DestPos;
 Table1.Post;
 Table1.GotoBookmark(BMDest);              //Goto to DestRecord
 Table1.Edit;                              //Update Pos From SourceRecord
 Table1.FieldByName('Pos').AsInteger := SourcePos;
 Table1.Post;
 Table1.FreeBookmark(BMSource);            //Forget the Rembering
 Table1.FreeBookmark(BMDest);
 Table1.EnableControls;                    //Show The Change
 InDrag := False;                          //Dragging done
end;


end.


meikl ;-)
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

763 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

7 Experts available now in Live!

Get 1:1 Help Now