?
Solved

RowMove within a DBGrid

Posted on 2000-05-12
15
Medium Priority
?
253 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
[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
  • 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
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.

 
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 800 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses
Course of the Month14 days, 20 hours left to enroll

771 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