Icon View Thread

The following is the text of the current message along with any replies.
Messages 1 to 7 of 7 total
Thread Dragging and Dropping within DBGrids
Sun, Jul 28 2013 6:15 PMPermanent Link

Michael Riley

ZilchWorks

Avatar

I'm having a very difficult time trying to incorporate dragging and
dropping within a DBGrid.

I thought I was all set when I found this article by Cary Jensen:
http://caryjensen.blogspot.com/2012/08/dragging-and-dropping-into-dbgrids.html

Jensen's technique only works if "Row 1" of the DBGrid matches "RecNo
1" of the dataset. If the DBGrid is scrolled down and "RecNo 1" is no
longer visible the drag and drop operation get all buggered-up. I've
left a comment on his blog and sent him an email. ( crickets )

I've been working on this for most of the weekend and can't figure out
how to make it work. I'm having one of my "I hate Delphi" days. Frown

Anyway, I'd like to use the native VCL DBGrid that comes with Delphi
instead of a third party component.

Have any of you been able to get a DBGrid to allow dragging rows from
one spot to another spot? If so would you be willing to share how you
did this?

Thanks,

--
Michael Riley
GySgt USMC (Ret)
http://capecodgunny.blogspot.com/
Mon, Jul 29 2013 4:08 AMPermanent Link

Abdulaziz Al-Jasser

Michael,

See http://delphi.about.com/library/weekly/aa042605a.htm

Regards,
Abdulaziz Jasser
Mon, Jul 29 2013 4:38 AMPermanent Link

Roy Lambert

NLH Associates

Team Elevate Team Elevate

Michael


I have it working well using my dbgrid descendent and my custom stringlist based dataset Smiley

Its a nightmare! I think the big boys manage it primarily through operating in unbound mode. It is not possible to achieve using live data with an index since its the index that depends on where it goes rather than where you drop it.

The big trick is working out what's under the cursor.

As you've found it works as long as the whole of the CDS is displayed visibly in the grid.

This is the code I use in my DBGrid descendent to get it to scroll

procedure TnlhDBGrid.DragOver(Source: TObject; X, Y: Integer; State: TDragState; var Accept: Boolean);
var
MovingPoint: integer;
begin
inherited;
if Accept and ((DataSource.DataSet.RecordCount * DefaultRowHeight) > Height) then begin
 MovingPoint := DefaultRowHeight div 2;
 if Y < MovingPoint then SendMessage(Self.Handle, WM_VSCROLL, SB_LINEUP, 0)
 else if Height - Y < MovingPoint then SendMessage(Self.Handle, WM_VSCROLL, SB_LINEDOWN, 0);
end;
end;

but testing in the code you refer to in the article it doesn't work, and that's because its assuming a simple relationship between the grid row and the dataset. I have this in my grid descendent, and its the crucial bit. I'm not sure it can be implemented outside a descendent.

function TnlhDBGrid.RecNoUnderMouse: integer;
var
mp: TPoint;
ARow: Integer;
OldActive: Integer;
begin
Result := -1;
if not DataLink.Active then Exit;
mp := ScreenToClient(Mouse.CursorPos);
ARow := MouseCoord(mp.X, mp.Y).Y;
if (ARow < TitleOffset) then Exit;
ARow := ARow - TitleOffset;
if ARow < 0 then Exit;
OldActive := DataLink.ActiveRecord;
DataLink.ActiveRecord := ARow;
Result := DataLink.DataSet.RecNo;
DataLink.ActiveRecord := OldActive;
end;

and this is the code I use in a form (well a frame actually) to manage the process

procedure TRulesEditorFrame.RuleListMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
if RuleList.ColumnUnderMouse < 0 then RuleList.BeginDrag(True, 0);
end;

procedure TRulesEditorFrame.RuleListDragDrop(Sender, Source: TObject; X, Y: Integer);
var
NewPosition: integer;
begin
if MoveRuleFrom >= 0 then begin
 NewPosition := RuleList.RecNoUnderMouse;
 if NewPosition > -1 then Rules.SwopRecords(MoveRuleFrom, NewPosition);
 MoveRuleFrom := -1;
end;
end;

procedure TRulesEditorFrame.RuleListDragOver(Sender, Source: TObject; X, Y: Integer; State: TDragState; var Accept: Boolean);
begin
Accept := (Sender = RuleList) and (RuleList.RecNoUnderMouse > -1)
end;

procedure TRulesEditorFrame.RuleListStartDrag(Sender: TObject; var DragObject: TDragObject);
begin
MoveRuleFrom := RuleList.RecNoUnderMouse;
end;

Roy Lambert
Mon, Jul 29 2013 6:28 AMPermanent Link

Michael Riley

ZilchWorks

Avatar

Abdulaziz Jasser wrote:

> Michael,
>
> See http://delphi.about.com/library/weekly/aa042605a.htm
>  
> Regards,
> Abdulaziz Jasser

That discusses dragging onto a dbgrid from another source. The dragging
and dropping I'm trying to get working stays with the dbgrid. For
example: dragging row 7 to row 2.

--
Michael Riley
GySgt USMC (Ret)
www.zilchworks.com
Sat, Aug 3 2013 11:57 PMPermanent Link

Michael Riley

ZilchWorks

Avatar

Michael Riley wrote:

> Jensen's technique only works if "Row 1" of the DBGrid matches "RecNo
> 1" of the dataset. If the DBGrid is scrolled down and "RecNo 1" is no
> longer visible the drag and drop operation get all buggered-up. I've
> left a comment on his blog and sent him an email. ( crickets )

FYI, I just heard from Cary Jensen regarding the bug in his DBGrid
drag/drop sample code. He has found a solution thanks to Uwe Raabe and
will be implementing it within the next couple weeks.

http://stackoverflow.com/questions/18034030/

--
Michael Riley
GySgt USMC (Ret)
www.zilchworks.com
Sun, Aug 4 2013 5:25 AMPermanent Link

Roy Lambert

NLH Associates

Team Elevate Team Elevate

Michael


I assume they know what they're talking about but it has me lost, partly because I don't understand what Value is and where its coming from.

Roy Lambert
Sun, Aug 4 2013 7:54 AMPermanent Link

Michael Riley

ZilchWorks

Avatar

Roy Lambert wrote:

> Michael
>
>
> I assume they know what they're talking about but it has me lost,
> partly because I don't understand what Value is and where its coming
> from.
>
> Roy Lambert

Roy,

Let's see what these Delphi big boys come up with. Wink

--
Michael Riley
GySgt USMC (Ret)
www.zilchworks.com
Image