Solved

RowMove within a DBGrid

Posted on 2000-05-12
15
249 Views
Last Modified: 2010-04-04
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
0
Comment
Question by:gulki
  • 6
  • 5
  • 4
15 Comments
 
LVL 27

Expert Comment

by:kretzschmar
ID: 2803853
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
 
LVL 9

Expert Comment

by:ITugay
ID: 2803883
Hi gulki, Meikl,

Drag & Drop will not help in this case?

-----
Igor.



0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 2803959
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
ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

 
LVL 9

Expert Comment

by:ITugay
ID: 2803979
Ok, meikl.
I have too. Let you first;)
----
Igor
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 2804000
hi igor,

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

meikl
0
 
LVL 9

Expert Comment

by:ITugay
ID: 2804034
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
 
LVL 9

Expert Comment

by:ITugay
ID: 2804211
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
 
LVL 27

Accepted Solution

by:
kretzschmar earned 200 total points
ID: 2806704
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
 
LVL 27

Expert Comment

by:kretzschmar
ID: 2806718
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
 

Author Comment

by:gulki
ID: 2808075
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
 

Author Comment

by:gulki
ID: 2849924
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
 
LVL 27

Expert Comment

by:kretzschmar
ID: 2850455
just accept the comment as answer, which helps you mostly ;-)
glad, that you like it
0
 
LVL 9

Expert Comment

by:ITugay
ID: 2853368
hi qulki,
meikl did a big work for you, of course you should accept his comment as answer;)
----
Igor
0
 

Author Comment

by:gulki
ID: 2856247
Comment accepted as answer
0
 

Author Comment

by:gulki
ID: 2856248
It was a really excellent job !

Thank you kretzschmar , you really earned the points
0

Featured Post

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

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

Suggested Solutions

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…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.

777 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