怎么能改变stringgrid的记录顺序,直接在stringgrid上拖拽。
1 aaaaa
2 bbbbb
3 ccccc
4 ddddd
5 eeeee
6 fffff
假设上面是2列,6行的一个grid。
如何能直接拖拽第六行,使它成为第一行,应该用stringgrid的那个事件来触发呀。

解决方案 »

  1.   


    {新建一个工程测试以下代码....凡本人答贴的代码, 99%已经过本人测试千万别说“能否再加个虚线框”......
    }unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls, Grids;type
      TMyStrGrid = class(TStringGrid)
        private
          FCanMove: Boolean;
          FFrom, FTo: integer;
          FStrs: TStringList;
        public
          procedure GridMouseDown(Sender: TObject; Button: TMouseButton;
            Shift: TShiftState; X, Y: Integer);
          procedure GridMouseMove(Sender: TObject; Shift: TShiftState; X,
            Y: Integer);
          procedure GridMouseUp(Sender: TObject; Button: TMouseButton;
            Shift: TShiftState; X, Y: Integer);
        published
          constructor Create(AOwner: TComponent); override;
          destructor Destory;
        end;  TForm1 = class(TForm)
        Button1: TButton;
        procedure FormCreate(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;var
      Form1: TForm1;implementation{$R *.dfm}{ TMyStrGrid }procedure TMyStrGrid.GridMouseDown(Sender: TObject;
      Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
    var
      I: integer;
    begin
      Self.FCanMove := True;
      FFrom := Self.Row;
      FStrs.Clear;
      for I := 0 to Self.ColCount-1 do
        FStrs.Add(Self.Cells[I, FFrom]);
    end;procedure TMyStrGrid.GridMouseMove(Sender: TObject; Shift: TShiftState;
      X, Y: Integer);
    var
      C, R: Integer;
    begin
      Self.MouseToCell(X, Y, C, R);
      if Self.FCanMove then FTo := R;
      Form1.Caption := InttoStr(FTo);
    end;procedure TMyStrGrid.GridMouseUp(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    var
      I: integer;
    begin
      if Self.FCanMove and (FFrom<>FTo) then begin
        MoveRow(FFrom, FTo);
        for I := 0 to Self.ColCount-1 do
          Self.Cells[I, FTo] := FStrs[I];
      end;
      Self.FCanMove := False;
    end;constructor TMyStrGrid.Create(AOwner: TComponent);
    begin
      inherited;
      FStrs := TStringList.Create;
      OnMouseDown := Self.GridMouseDown;
      OnMouseMove := Self.GridMouseMove;
      OnMouseUp := Self.GridMouseUp;
    end;destructor TMyStrGrid.Destory;
    begin
      FStrs.Free;
      inherited;
    end;{TForm1}procedure TForm1.FormCreate(Sender: TObject);
    var
      I, J: integer;
      MyGrid: TMyStrGrid;
    begin
      MyGrid := TMyStrGrid.Create(Self);
      with MyGrid do begin
        Top := 30;
        Left := 30;
        Width := 500;
        Height := 300;
        Parent := Self;
      end;
      for I := 1 to MyGrid.ColCount-1 do
        for J := 1 to MyGrid.RowCount-1 do
          MyGrid.Cells[I,J] := Format('%d,%d',[I,J]);
    end;end.
      

  2.   

    lihuasoft学习