Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

DBGrid drag&drop

Posted on 2002-06-09
1
Medium Priority
?
767 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
[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
1 Comment
 
LVL 27

Accepted Solution

by:
kretzschmar earned 150 total points
ID: 7066226
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

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

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…
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Suggested Courses

722 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