RowMove within a DBGrid

Hi to all experts

Does someone know a possibility to move a row within a dbgrid with the mouse (like columnmove).

The thing is, that I have a column called "position" with represents the sortorder of the query ! My question is now, if there is a possibility, that a user can move a row (position) with the mouse, and if he has done so, a function automatically changes in the database the values of the field position depending on the new order now !


Thanks
gulkiAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
kretzschmarConnect With a Mentor Commented:
hi both,

my prototype (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
 
kretzschmarCommented:
hi gulki,
you must "move" (adjust the positionsvalue) in your table.

therefore there is no way to do it first in the grid and then in the table, its more first do it in the table and the grid recogize the change

can provide a sample in some hours

meikl
0
 
ITugayCommented:
Hi gulki, Meikl,

Drag & Drop will not help in this case?

-----
Igor.



0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
kretzschmarCommented:
hi igor,

yes, i've ignored the drag&drop for now,
(but i guess, i have also a solution for this), because first is to implement a "recordmove" in the underlying dataset, which is not to hard and which i have already done sometimes ago.

meikl ;-)
0
 
ITugayCommented:
Ok, meikl.
I have too. Let you first;)
----
Igor
0
 
kretzschmarCommented:
hi igor,

you must not wait,
also i'm interesting
how you it implements.

meikl
0
 
ITugayCommented:
hi meikl,
litle bit later, I'm still trying to help
http://www.experts-exchange.com/jsp/qShow.jsp?ta=delphi&qid=10347920 

Igor.
0
 
ITugayCommented:
Hi qulki,
There is the way how to change order of records in database table. Let suppose you have a table where "RCN" field is sortorder.

RCN  DATA
 1     aaa
 2     bbb
 3     ccc
 4     ddd
 5     eee
 6     fff

And you need to move record "5 eee" to top, between "3 ccc" and "2 bbb".

1.  hide record
SQL> update thetable set RCN=0 where RCN=5

2.  make hole after "2 bbb" and increase sortorder to other records
SQL> update thetable set RCN=RCN+1 where RCN>2 and RCN<5

3. place to desired position
SQL> update thetable set RCN=3 where RCN=0


in most cases:
OLDPOS - old value of RCN
NEWPOS - new value of RCN

update thetable set RCN=0 where RCN=:OLDPOS
update thetable set RCN=RCN+1 where RCN>:NEWPOS-1 and RCN<:OLDPOS
update thetable set RCN=:NEWPOS where RCN=0




----
Igor.
0
 
kretzschmarCommented:
just to remark,

if you have a live query then you can replace table1 with your query,
if it not a live query then you must do it like Igor comments
(three update-sql-statements) and
close and reopen your original query after the action

meikl
0
 
gulkiAuthor Commented:
Hi Folks

I am now triying your proposals. In a few days I will tell you, if it worked or not !

thanks in advance
0
 
gulkiAuthor Commented:
Thanks a lot folks

You helped me a lot. And I think, that the prototype-solution of kretzschmar has earned the points, even if igor had an idea how to change the order, but the question was a drag&drop.

Thanks
0
 
kretzschmarCommented:
just accept the comment as answer, which helps you mostly ;-)
glad, that you like it
0
 
ITugayCommented:
hi qulki,
meikl did a big work for you, of course you should accept his comment as answer;)
----
Igor
0
 
gulkiAuthor Commented:
Comment accepted as answer
0
 
gulkiAuthor Commented:
It was a really excellent job !

Thank you kretzschmar , you really earned the points
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.