1
votes

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

2
Please explain why you need the middle row number?fpiette
Ricky, you have already got an answer that you have marked as the correct one, but I would still ask you to clarify this: Do you mean the middle grid row number - 11 in your example as your grid can display 21 data rows? Or, do you mean the data set record number, that is displayed in the middle row, which happens to be 11 in the example as it is not scrolled? But could be e.g. 21 if the displayed data set rows are 11 - 31. I ask, as you emphasized that the grid is in scrolled state, which would be signifiant only in the latter case.Tom Brunberg
@TomBrunberg Yes Tom. 11 is an example only. It really depends on what visible scroll state. It could be a different number. I am referring to the grid display not from the dataset.RickyBelmont
@fpiette I need the middle row number because there is the procedure I made when it goes after the middle row. It's unique requirement of mine but now is working.RickyBelmont

2 Answers

2
votes

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.

1
votes

The row count of the grid is given by the formula:

RowCount := DBGrid1.ClientHeight div (RowHeight + 1);

The middle row is obviously half that value (Also depends if you count the title row or not).

RowHeight is not published in DBGrid. To get hand on it, you can use an interposer class:

type
    TInterDBGrid = class(TDBGrid);

And use it like this:

RowHeight := TInterDBGrid(DBGrid1).RowHeights[0];

The interposer class must be defined before the form.