delphidatagridscrolldelphi-xe2vcl

How to enable mouse wheel scrolling on a TDBCtrlGrid?


TDBCtrlGrid does not react to the mouse wheel at all.

I tried this:

procedure TForm1.FormMouseWheel(Sender: TObject;
  Shift: TShiftState; WheelDelta: Integer; MousePos: TPoint;
  var Handled: Boolean);
begin
  if DBCtrlGrid1.ClientRect.Contains(DBCtrlGrid1.ScreenToClient(MousePos)) then
  begin
    DBCtrlGrid1.ScrollBy(0, WheelDelta);
    Handled := True;
  end;
end;

The control grid now scrolls, but it does not change the position in the DataSet, but instead moves its content out of the client rect which looks pretty ugly.

How do I enable mouse wheel scrolling on a TDBCtrlGrid?


Solution

  • As a workaround you can scroll the DataSet instead:

    procedure TForm1.FormMouseWheel(Sender: TObject;
      Shift: TShiftState; WheelDelta: Integer; MousePos: TPoint;
      var Handled: Boolean);
    var
      I: Integer;
      Grid: TDBCtrlGrid;
      DataSet: TDataSet;
    begin
      Grid := DBCtrlGrid1;
      if not Grid.ClientRect.Contains(Grid.ScreenToClient(MousePos)) then
        Exit;
      if not Assigned(Grid.DataSource) then
        Exit;
      DataSet := Grid.DataSource.DataSet;
      if DataSet = nil then
        Exit;
      for I := 0 to Abs(WheelDelta div 256) - 1 do 
      begin
        if WheelDelta > 0 then
          DataSet.Prior
        else
          DataSet.Next;
      end;
      Handled := True;
    end;