본문 바로가기

Delphi/꿀팁

TcxGrid에서 드래그드랍으로 레코드 바꾸기

<cxGridTableView>의 DragMode를 dmAutomatic으로 변경.

procedure TForm1.cxGridTableView1DragOVer(Sender, Source: TObject; 
  X, Y: Integer; State: TDragState; var Accept: Boolean);
begin
  //드래그 허용 조건 설정하기. 현재는 드래그드롭 타겟이 자기자신일 경우만 True
  Accept := Sender = TcxGridSite(TcxDragControlObject(Source).Contorl);
end;
procedure TForm1.cxGridTableView1StartDrag(Sender: TObject; var DragObject: TDragObject);
begin
  //Source RecordIndex 저장
  FPrevRecordIndex := TcxGridTableView(TcxGridSite(Sender).GridView).Controller.SelectedRecords[0].RecordIndex;
end;
procedure TForm1.cxGridTableViewDragDrop(Sender, Source: TObject; X, Y: Integer);
var
  I: Integer;
  ColumnValue: Variant;
  
  SenderSite: TcxGridSite;
  SenderHitTest: TcxGridRecordCellHitTest;
begin
  SenderSite := TcxGridSite(Sender);
  SenderHitTest := TcxGridTableView(SenderSite.GridView).ViewInfo.GetHitTest(Point(X, Y)) as TcxGridRecordCellHitTest;
  
  //Dst Record 체크
  if SenderHitTest = nil then
    Exit;
    
  if (PrevRecordIndex > -1) and (SenderHitTest.GridRecord.RecordIndex <> PrevRecordIndex) then begin
    TcxGridTableView(SenderSite.GridView).BeginUpdate;
    try
      //Swap
      for I := 0 to TcxGridTableView(SenderSite.GridView).ColumnCount - 1 do begin
        ColumnValue := SenderHitTest.GridRecord.Values[I];
        SenderHitTest.GridRecord.Values[I] := TcxGridTableView(SenderSite.GridView).DataController.Values[FPrevRecordIndex, I];
        TcxGridTableView(SenderSite.GridView).DataController.Values[FPrevRecordIndex, I] := ColumnValue;
      end;
    finally
      TcxGridTableView(SenderSite.GridView).EndUpdate;
      PrevRecordIndex := -1;
    end;
  end;
end;
반응형