cxGrid_Q31584 cxgrid 拖放移动记录

cxgrid 拖放移动记录,cxgrid 拖放,cxgrid 拖动记录,cxgrid 鼠标拖动记录 这是cxgrid开发公司回复客户时所发送的源码项目,用于实现鼠标拖动记录,改变记录在表格中的位置,所以直接编译运行,供大家借鉴 

问题地址:https://www.devexpress.com/Support/Center/Question/Details/Q31584/help-with-drag-and-drop-in-cxgrid-reorder-rows

 下载地址:https://www.devexpress.com/Support/Center/Attachment/GetAttachmentFile/e1b941a6-feb1-413e-b7e1-b1c1eece12be

 

 

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, cxStyles, cxCustomData, cxGraphics, cxFilter, cxData,
  cxDataStorage, cxEdit, DB, cxDBData, cxGridCustomTableView,
  cxGridTableView, cxGridDBTableView, DBTables, cxGridLevel, cxClasses,
  cxControls, cxGridCustomView, cxGrid, dxSkinsCore,  StdCtrls;

type
  TForm1 = class(TForm)
    cxGrid1: TcxGrid;
    cxGrid1DBTableView1: TcxGridDBTableView;
    cxGrid1Level1: TcxGridLevel;
    DataSource1: TDataSource;
    Table1: TTable;
    cxGrid1DBTableView1ID: TcxGridDBColumn;
    cxGrid1DBTableView1Order: TcxGridDBColumn;
    cxGrid1DBTableView1Text: TcxGridDBColumn;
    Table1ID: TAutoIncField;
    Table1Order: TSmallintField;
    Table1Text: TStringField;
    Button1: TButton;
    procedure cxGrid1DBTableView1DragOver(Sender, Source: TObject; X,
      Y: Integer; State: TDragState; var Accept: Boolean);
    procedure cxGrid1DBTableView1DragDrop(Sender, Source: TObject; X,
      Y: Integer);
    procedure FormCreate(Sender: TObject);
    procedure cxGrid1DBTableView1StartDrag(Sender: TObject;
      var DragObject: TDragObject);
    procedure Button1Click(Sender: TObject);
  private
    AKeys: Variant;
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.cxGrid1DBTableView1DragOver(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;

procedure TForm1.cxGrid1DBTableView1DragDrop(Sender, Source: TObject; X,
  Y: Integer);
var
  HT: TcxCustomGridHitTest;
  ASourceMinOrderValue,ASourceMaxOrderValue,ADestOrderValue: Variant;
  I: Integer;
  AController : TcxGridTableController;
  AOrderIndex : Integer;
  ADataSet : TDataset;
  AField : TField;
  IsUp : boolean;
  ADataController :  TcxDBDataController;
begin
  AOrderIndex := cxGrid1DBTableView1Order.Index;
  with TcxGridSite(Sender) do
  begin
    HT := ViewInfo.GetHitTest(X, Y);
    ADestOrderValue := TcxGridRecordCellHitTest(HT).GridRecord.Values[AOrderIndex];
    AController := TcxGridTableController(GridView.Controller);
    ADataController :=  TcxDBDataController(GridView.DataController);
    ASourceMinOrderValue := AController.SelectedRows[0].Values[AOrderIndex];
    ASourceMaxOrderValue := AController.SelectedRows[AController.SelectedRowCount-1].Values[AOrderIndex];
    ADataSet :=  ADataController.DataSet;
    AField := ADataSet.FieldByName('Order');
    IsUp := (ADestOrderValue < ASourceMinOrderValue);

    ADataSet.First;
    for I := 0 to ADataSet.RecordCount - 1 do
    begin
      ADataSet.Edit;
      if IsUp and (AField.AsInteger >= ADestOrderValue) and (AField.AsInteger < ASourceMinOrderValue) then
        AField.AsInteger := AField.AsInteger + AController.SelectedRowCount;
      if not IsUp and (AField.AsInteger <= ADestOrderValue) and (AField.AsInteger > ASourceMaxOrderValue) then
          AField.AsInteger := AField.AsInteger - AController.SelectedRowCount;  
      ADataSet.Post;
      ADataSet.Next;
    end;
 
    if Not IsUp then
      ADestOrderValue := ADestOrderValue - AController.SelectedRowCount + 1; 

    for I := 0 to AController.SelectedRowCount - 1 do
    begin
      TcxDBDataController(GridView.DataController).LocateByKey(AKeys[I]);
      ADataset.Edit;
      AField.Value := ADestOrderValue + I;
      ADataset.Post;
    end;

  end;
end;



procedure TForm1.FormCreate(Sender: TObject);
begin
  Table1.Open;
end;



procedure TForm1.cxGrid1DBTableView1StartDrag(Sender: TObject;
  var DragObject: TDragObject);
var
  I: Integer;
begin
  with TcxGridDBTableView(TcxGridSite(Sender).GridView) do
  begin
    AKeys := VarArrayCreate([0, Controller.SelectedRecordCount - 1], varVariant);
    for I := 0 to Controller.SelectedRecordCount - 1 do
      AKeys[I] := Controller.SelectedRecords[I].Values[cxGrid1DBTableView1ID.Index];
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  with Table1 do
  begin
    DisableControls;
    try
      First;
      while not Eof do
      begin
        Edit;
        FieldValues['Order'] := FieldValues['ID']; 
        Next;
      end;
    finally
      EnableControls;
    end;
  end;
end;

end.

 

 

DFM

object Form1: TForm1
  Left = 438
  Top = 141
  Caption = 'Form1'
  ClientHeight = 430
  ClientWidth = 465
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = False
  OnCreate = FormCreate
  PixelsPerInch = 96
  TextHeight = 13
  object cxGrid1: TcxGrid
    Left = 0
    Top = 0
    Width = 465
    Height = 430
    Align = alClient
    TabOrder = 0
    object cxGrid1DBTableView1: TcxGridDBTableView
      DragMode = dmAutomatic
      OnDragDrop = cxGrid1DBTableView1DragDrop
      OnDragOver = cxGrid1DBTableView1DragOver
      OnStartDrag = cxGrid1DBTableView1StartDrag
      NavigatorButtons.ConfirmDelete = False
      DataController.DataSource = DataSource1
      DataController.KeyFieldNames = 'ID'
      DataController.Summary.DefaultGroupSummaryItems = <>
      DataController.Summary.FooterSummaryItems = <>
      DataController.Summary.SummaryGroups = <>
      OptionsSelection.CellSelect = False
      OptionsSelection.MultiSelect = True
      object cxGrid1DBTableView1ID: TcxGridDBColumn
        DataBinding.FieldName = 'ID'
        Options.Sorting = False
      end
      object cxGrid1DBTableView1Order: TcxGridDBColumn
        DataBinding.FieldName = 'Order'
        Options.Sorting = False
        SortIndex = 0
        SortOrder = soAscending
      end
      object cxGrid1DBTableView1Text: TcxGridDBColumn
        DataBinding.FieldName = 'Text'
        Options.Sorting = False
      end
    end
    object cxGrid1Level1: TcxGridLevel
      GridView = cxGrid1DBTableView1
    end
  end
  object Button1: TButton
    Left = 360
    Top = 48
    Width = 75
    Height = 25
    Caption = 'Reset Order'
    TabOrder = 1
    OnClick = Button1Click
  end
  object DataSource1: TDataSource
    DataSet = Table1
    Left = 80
    Top = 144
  end
  object Table1: TTable
    DatabaseName = '.\'
    TableName = 'TestTable.db'
    Left = 33
    Top = 144
    object Table1ID: TAutoIncField
      FieldName = 'ID'
      ReadOnly = True
    end
    object Table1Order: TSmallintField
      FieldName = 'Order'
    end
    object Table1Text: TStringField
      FieldName = 'Text'
      Size = 25
    end
  end
end

 

posted @ 2017-07-18 17:24  周黔  阅读(831)  评论(0编辑  收藏  举报