我对 DBGrid 垂直滚动有疑问。当我用鼠标滚轮或垂直滚动​​条垂直滚动它时,它会上下移动选定的行。我想让它滚动不是选定的行而是整个网格。就像它在 Microsoft Excel 中的工作原理一样(只是为了让您知道我的意思)。有什么建议么?

有帮助吗?

解决方案 2

嗯,差不多就是我想看到的了。在 swissdelhicenter.ch 上找到了 hanuleye 的帖子。此代码让您可以使用鼠标滚轮自由滚动 DBGrid。

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, Grids, DBGrids, DB, DBTables;

type
  TForm1 = class(TForm)
    DataSource1: TDataSource;
    Table1: TTable;
    DBGrid1: TDBGrid;
    procedure FormCreate(Sender: TObject);
    procedure DBGridMouseWheel(Sender: TObject; Shift: TShiftState;
      WheelDelta: Integer; MousePos: TPoint; var Handled: Boolean);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

  TWheelDBGrid = class(TDBGrid)
  public
    property OnMouseWheel;
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
  TWheelDBGrid(DBGrid1).OnMouseWheel := DBGridMouseWheel;
end;

function GetNumScrollLines: Integer;
begin
  SystemParametersInfo(SPI_GETWHEELSCROLLLINES, 0, @Result, 0);
end;

procedure TForm1.DBGridMouseWheel(Sender: TObject; Shift: TShiftState;
  WheelDelta: Integer; MousePos: TPoint; var Handled: Boolean);
var
  Direction: Shortint;
begin
  Direction := 1;
  if WheelDelta = 0 then
    Exit
  else if WheelDelta > 0 then
    Direction := -1;

  with TDBGrid(Sender) do
  begin
    if Assigned(DataSource) and Assigned(DataSource.DataSet) then
      DataSource.DataSet.MoveBy(Direction * GetNumScrollLines);
    Invalidate;
  end;
end;

end.

其他提示

我认为这是不可能的,因为对我来说,DBGrid 上的滚动条更像是进度指示器而不是滚动条。它的行为与 ListView 中滚动“页面”的行为不同,在数据库控件中,即使您向上或向下移动一行,滚动条也会更改以反映“当前行”/“总行”分数

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top