我的DBGrid垂直滚动有问题。当我使用鼠标滚轮或垂直滚动条垂直滚动它时,它会向上和向下移动选定的行。我想让它滚动而不是选择行而是整个网格。就像它在Microsoft Excel中工作一样(只是为了让你知道我的意思)。有什么建议吗?
答案 0 :(得分:0)
我不认为这是可能的,因为对我而言,DBGrids上的滚动条似乎更像是进度指示器而不是滚动条。它的行为与ListViews中滚动“页面”的滚动不同,在db控件中,即使向上或向下移动单行,滚动条也会更改以反映“当前行”/“总行”分数
答案 1 :(得分:0)
好吧,几乎我想看到的。在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.