v
Not logged inv
SearchAsk a QuestionReport an IssueMake a SuggestionMy Questions and Issues
KB Article
Find By ID

How to implement drag-and-drop inside an unbound Grid

Article Details

K18210
6
10/25/2011
VCL
ExpressQuantumGrid Suite

I have an unbound Grid. I want to implement the drag-and-drop functionality there. How can I do this?

Solution

To implement this functionality, you can use the <aView>.DataController.CopyRecord method. However, this method is in the protected section of the DataController. So, it is necessary to create an accessor to access it:

[Delphi]

type

  TcxGridDataControllerAccess = class (TcxGridDataController);


Also, you should handle the view's OnStartDrag, OnDragOver and OnDragDrop events, as shown in the following code:

[Delphi]

var

  FSourceRecordIndex, FDestRecordIndex: integer;



procedure cxGridTableViewStartDrag(Sender: TObject;

  var DragObject: TDragObject);

begin

  with TcxGridTableView(TcxGridSite(Sender).GridView) do

    FSourceRecordIndex := Controller.FocusedRecord.Index; //Getting the source record index

end;



procedure cxGridTableViewDragOver(Sender, Source: TObject; X,

  Y: Integer; State: TDragState; var Accept: Boolean);

var

  HT: TcxCustomGridHitTest;

begin

  with TcxGridSite(Sender) do

  begin

    HT := ViewInfo.GetHitTest(X, Y);

    Accept := (HT is TcxGridRecordCellHitTest) and (TcxGridRecordCellHitTest(HT).GridRecord.RecordIndex <> GridView.DataController.FocusedRecordIndex)

  end;

end;



type

  TcxGridDataControllerAccess = class (TcxGridDataController); //creating an accessor to access the CopyRecord method



procedure cxGridTableViewDragDrop(Sender, Source: TObject; X,

  Y: Integer);

var

  HT: TcxCustomGridHitTest;

  ANewIndex: integer;

  ADeleteIndex: integer;

begin

  with TcxGridSite(Sender) do

  begin

    HT := ViewInfo.GetHitTest(X, Y);

    FDestRecordIndex := TcxGridRecordCellHitTest(HT).GridRecord.Index; //Destination record index

    with TcxGridDataControllerAccess(GridView.DataController) do

      begin

        if (FSourceRecordIndex >= RecordCount) or (FSourceRecordIndex < 0) then

          Exit;

        BeginUpdate;

        try

          ANewIndex := InsertRecord(FDestRecordIndex + 1); //Inserting new record after the destination record

          if FSourceRecordIndex > ANewIndex then //determining the shifted position of the source record

            inc(FSourceRecordIndex);



          CopyRecord(FSourceRecordIndex, ANewIndex); //Copying record values

          if FSourceRecordIndex < ANewIndex then //determining the shifted position of the inserted record after deleting the source record

            dec(ANewIndex);

          DeleteRecord(FSourceRecordIndex);

        finally

        FocusedRecordIndex := ANewIndex; //Focusing inserted record

        EndUpdate;

      end;

  end;

  end;

end;

In the attachment you will find an example that demonstrates this approach.

See also:
How to implement drag-and-drop using ExpressQuantumGrid 4
How to implement drag-and-drop inside a Grid
How to obtain the Grid row at a given location on the screen
How to drag a cell's value to an external editor
Why does my custom DragObject not function?
How to allow an end-user to perform a drag and drop operation with a specific criteria

Attachments

Do you have any comments? We are eager to hear them!

0 people have rated this page
Post Your Vote (Login Required)
v
v
Search
Searching Tips