Is there a way of finding out which row is current in a TDBGrid?
3 Answers
I'm not sure if I understand your question, but I'll attempt an answer and maybe you can clarify if this isn't what you are asking.
Since a TDBGrid is tied to a DataSource, the current row is the same as the current row in the data source. You can query the DataSource, either by looking at a primary key value or the RecNo property to determine which record is the current one.

- 3,567
- 3
- 24
- 21
You can do it like this:
1 - Define a local class that is a copy of TDBGrid (this will let you access private methods):
type
THackDBGrid = class(TDBGrid);
2 - Then you can cast to your locally defined class, and pull from private methods as in:
function TfrmMain.GetFieldValue(colnum : integer): string;
begin
Result := THackDBGrid(grdMain).GetFieldValue(colnum);
end;
Or, to get the row #:
function CurrentRowNumber: integer;
Result := THackDBGrid(grdMain).Row;
end;
This technique is useful in other situations, too, but I cannot claim credit. I got it from here.

- 57,317
- 63
- 160
- 234
-
This is a great way to work around the rule of having everything in one file to access private items from ancestor classes. – Joseph Poirier Mar 23 '20 at 15:28
If you don't want to depend on the data source to provide a record count and a current record (for example because you are using a unidirectional cursor on a remote server), then the technique of accessing the properties of the underlying grid control can be used.
See this answer to a similar StackOverflow question.
-
You started to say the correct thing, but the link you indicated only shows how to get the recordcount but the most important (RecNo or Row or CurrentRow, etc.) cannot be obtained directly from the grid. Sorry – Carlos B. Feitoza Filho Mar 02 '13 at 19:15
-
@Carlos: What's stopping you from writing `Row := TDummyGrid(MyDBGrid).Row` instead of `RowCount := TDummyGrid(MyDBGrid).RowCount`? A tiny bit of thinking is required, you know... – mghie Mar 03 '13 at 10:42
-
Well... I want to show alternate colors on my Grid, but, the ROW property is not "persistent" between the cursor changes, ie, if you move the cursor top or down on the grid, changing the selection, the ROW property changes for ALL visible rows at same time. I need a way to identify each line individually, independently the current selected row. Now I'm using the RecNo of the linked dataset, but on unidirectional datasets, this property is useless. As you can see, A TINY BIT OF THINKING IS REQUIRED, but YOU was the lazy. Sorry... – Carlos B. Feitoza Filho Mar 06 '13 at 04:12
-
1@Carlos: You're showing a lack of understanding of the grid behaviour, is all. Alternate colours can simply be implemented by using the event parameters of the grid, which include the row and column of the cell to be painted. This has nothing to do with the *current* row or column. Down-voting an answer because it doesn't fit your own problem (which is different from the original question) is just grand. – mghie Mar 06 '13 at 04:47