Solved

RowMove within a DBGrid

Posted on 2000-05-12
15
245 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
 
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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

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…
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.

762 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

18 Experts available now in Live!

Get 1:1 Help Now