Search code examples
delphidbgrid

How to get the middle row number in the current DBGrid View


Can anyone help me get the middle row number of the current DBGrid view?

I was able to get the following but could not get the row number of it. I also do not know what is this for.

DBGRid1.CenterCurRowInView

Update: By the way, the grid is in scrolled view state.

Adding Screenshot:

enter image description here


Solution

  • My answer here shows how to determine the current row number and number of rows in a DBGrid:

    type
      TMyDBGrid = Class(TDBGrid);
    
    function TForm1.GetGridRow: Integer;
    begin
      Result := TmyDBGrid(DBGrid1).Row;
    end;
    
    function TForm1.GridRowCount : Integer;
    begin
      Result := TmyDBGrid(DBGrid1).RowCount;
    end;
    

    This avoids the need for a class helper and will work in Delphi versions which pre-date support for them.

    Btw, the grid only has a unique "middle row" if the number of rows being displayed in the grid is odd, of course. Also be careful of what you actually need, because the "middle row" is ambiguous if the number of rows in the dataset is less than the number of rows the grid can simultaneously display.