Scroll in grid without loosing my selection

Discussion of open issues, suggestions and bugs regarding MyDAC (Data Access Components for MySQL) for Delphi, C++Builder, Lazarus (and FPC)
Post Reply
deca
Posts: 19
Joined: Thu 05 Apr 2007 13:36
Location: Germany

Scroll in grid without loosing my selection

Post by deca » Wed 27 Aug 2008 11:46

Hi @ all,

I use the TCRDBGrid in my application. To scroll with the mouse through the grid and not loose my record selection I use the follwowing function:

Code: Select all

procedure TfmMain.AppMessage(var Msg: TMsg; var Handled: Boolean);
var DBGrid : TDBGrid;
		CRDBGrid : TCRDBGrid;
begin
 	if not fmMain.Showing then Exit;
  {Mouse wheel behaves strangely with dbgrids - this proc sorts this out}
  if Msg.message = WM_MOUSEWHEEL then
  begin
    if (Screen.ActiveControl is TDBGrid) or (Screen.ActiveControl is TCRDBGrid)then
    begin
      if Screen.ActiveControl is TDBGrid then
      begin
      	DBGrid := Screen.ActiveControl as TDBGrid;

        if Msg.wParam > 0 then
          Msg.wParam := SendMessage(DBGrid.Handle, WM_VSCROLL, SB_LINEUP, 0)
        else Msg.wParam := SendMessage(DBGrid.Handle, WM_VSCROLL, SB_LINEDOWN, 0);
      end
      else if Screen.ActiveControl is TCRDBGrid then
      begin
      	CRDBGrid := Screen.ActiveControl as TCRDBGrid;

        if Msg.wParam > 0 then
          Msg.wParam := SendMessage(CRDBGrid.Handle, WM_VSCROLL, SB_LINEUP, 0)
        else Msg.wParam := SendMessage(CRDBGrid.Handle, WM_VSCROLL, SB_LINEDOWN, 0);
      end;
      Handled := False;
    end;
  end;
end;
As you can see I also use this function with TDBGrid and its working very well. But in connection with the TCRDBGrid nothing happens. All selections are gone with scrolling.

Thanks in advance, DECA

Dimon
Devart Team
Posts: 2910
Joined: Mon 05 Mar 2007 16:32

Re: Scroll in grid without loosing my selection

Post by Dimon » Wed 27 Aug 2008 13:24

You should use the following code:

Code: Select all

procedure TfmMain.AppMessage(var Msg: TMsg; var Handled: Boolean);
var DBGrid : TDBGrid;
		CRDBGrid : TCRDBGrid;
begin
 	if not fmMain.Showing then Exit;
  {Mouse wheel behaves strangely with dbgrids - this proc sorts this out}
  if Msg.message = WM_MOUSEWHEEL then
  begin
    if (Screen.ActiveControl is TDBGrid) or (Screen.ActiveControl is TCRDBGrid)then
    begin
      if Screen.ActiveControl is TDBGrid then
      begin
      	DBGrid := Screen.ActiveControl as TDBGrid;

        if Msg.wParam > 0 then
          Msg.wParam := SendMessage(DBGrid.Handle, WM_VSCROLL, SB_LINEUP, 0)
        else Msg.wParam := SendMessage(DBGrid.Handle, WM_VSCROLL, SB_LINEDOWN, 0);
        Handled := False;
      end
      else if Screen.ActiveControl is TCRDBGrid then
      begin
      	CRDBGrid := Screen.ActiveControl as TCRDBGrid;

        if Msg.wParam > 0 then
          Msg.wParam := SendMessage(CRDBGrid.Handle, WM_VSCROLL, SB_LINEUP, 0)
        else Msg.wParam := SendMessage(CRDBGrid.Handle, WM_VSCROLL, SB_LINEDOWN, 0);
        Handled := True;
      end;
    end;
  end;
end;

deca
Posts: 19
Joined: Thu 05 Apr 2007 13:36
Location: Germany

Post by deca » Wed 27 Aug 2008 13:30

Thanks, its working. But why does it behave different to the TDBGrid?

Dimon
Devart Team
Posts: 2910
Joined: Mon 05 Mar 2007 16:32

Post by Dimon » Thu 28 Aug 2008 09:46

TCRDBGrid has own handler of the WM_MOUSEWHEEL message. As you create a new handler of this event, old handler must be overlayed.

Post Reply