Forum > Databases

[SOLVED] DBGrid - Center selected row.

<< < (2/3) > >>

Handoko:

--- Quote from: Zvoni on November 24, 2021, 11:36:54 am ---
--- Quote from: pcurtis on November 24, 2021, 11:27:18 am ---TopRow is not a property of TDBGrid

--- End quote ---
It's protected in TCustomGrid.

--- End quote ---

Not a problem, we can force a protected property to be used on its descendants.


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---type  TMyGrid = class(TCustomGrid)    public      property TopRow;  end; procedure TForm1.Button1Click(Sender: TObject);begin  TMyGrid(DBGrid1).TopRow := 5;end;
Only warning but compile-able. Unfortunately, due to some unknown reason it does not work.

Zvoni:

--- Quote from: Handoko on November 24, 2021, 03:09:51 pm ---Not a problem, we can force a protected property to be used on its descendants.


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---type  TMyGrid = class(TCustomGrid)    public      property TopRow;  end; procedure TForm1.Button1Click(Sender: TObject);begin  TMyGrid(DBGrid1).TopRow := 5;end;
Only warning but compile-able. Unfortunately, due to some unknown reason it does not work.

--- End quote ---
In Singlestep-Mode, when assigning (your Line 9), can you step into the (protected) setter of the Property?

Handoko:
Just tried it, yes I can step into it. Unfortunately the code is too advanced, I am not able to digest it.


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---procedure TCustomGrid.TryScrollTo(aCol, aRow: Integer; ClearColOff,  ClearRowOff: Boolean);var  TryTL: TPoint;  NewCol,NewRow: Integer;  TLChange: Boolean;begin  TryTL:=ScrollGrid(False,aCol, aRow);  TLChange := not PointIgual(TryTL, FTopLeft);  if TLChange  or (not PointIgual(TryTL, Point(aCol, aRow)) and (goSmoothScroll in Options))  or (ClearColOff and (FGCache.TLColOff<>0))  or (ClearRowOff and (FGCache.TLRowOff<>0)) then  begin    NewCol := TryTL.X - FTopLeft.X + Col;    NewRow := TryTL.Y - FTopLeft.Y + Row;    FTopLeft:=TryTL;    if ClearColOff then      FGCache.TLColOff := 0;    if ClearRowOff then      FGCache.TLRowOff := 0;    if (aCol>TryTL.X) and (goSmoothScroll in Options) then      FGCache.TLColOff := FGCache.MaxTLOffset.X;    if (aRow>TryTL.Y) and (goSmoothScroll in Options) then      FGCache.TLRowOff := FGCache.MaxTLOffset.Y;    {$ifdef dbgscroll}    DebugLn('TryScrollTo: TopLeft=%s NewCol=%d NewRow=%d',      [dbgs(FTopLeft), NewCol, NewRow]);    {$endif}    // To-Do: move rect with ScrollBy_WS and invalidate only new (not scrolled) rects    if TLChange then      doTopleftChange(False)    else      VisualChange;    if goScrollKeepVisible in Options then      MoveNextSelectable(False, NewCol, NewRow);  end;end;

pcurtis:
@Handoko PERFECT

pcurtis:
I made this custom DBGrid based on your supplied code


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---unit MyDBGrid; {$mode objfpc}{$H+} interface uses  Classes, SysUtils, LResources, Forms, Windows, Controls, Graphics, StdCtrls, ExtCtrls,  Dialogs, DBGrids, Grids; type  TMyHackedGrid = class(TDBGrid);  TMyDBGrid = class(TDBGrid)  private   protected   public    function GetSelectedRow : Integer;    function GetVisibleRowCount : Integer;    procedure ScrollTo(aIndexField : String; aValue : Integer; aOffset : Integer);    constructor Create(AOwner : TComponent); override;    destructor Destroy; override;  published   end; procedure Register; implementation {$R mydbgrid.res} constructor TMyDBGrid.Create(AOwner : TComponent);begin  inherited Create(AOwner);  AlternateColor := clMoneyGreen;  Color := clCream;  Options := Options + [dgRowSelect];  Options := Options - [dgEditing];  AutoFillColumns := true;end; destructor TMyDBGrid.Destroy;begin  inherited Destroy;end; function TMyDBGrid.GetSelectedRow : Integer;begin  Result := TMyHackedGrid(Self).Row;end; function TMyDBGrid.GetVisibleRowCount : Integer;begin  Result := TMyHackedGrid(Self).VisibleRowCount;end; procedure TMyDBGrid.ScrollTo(aIndexField : String; aValue : Integer; aOffset : Integer);var  iTemp : Integer;begin  if (not Assigned(DataSource)) or     (DataSource.DataSet.Active = False) or     (DataSource.DataSet.RecordCount = 0) then       exit;   DataSource.DataSet.First;  DataSource.DataSet.Locate(aIndexField, aValue, []);   for iTemp := 1 to aOffset do  begin    PostMessage(Self.Handle, WM_KEYDOWN, VK_UP, 0);  end;   for iTemp := 1 to aOffset do  begin    PostMessage(Self.Handle, WM_KEYDOWN, VK_DOWN, 0);  end;end; procedure Register;begin  RegisterComponents('Misc',[TMyDBGrid]);end; end. 

Navigation

[0] Message Index

[#] Next page

[*] Previous page

Go to full version