tags:

views:

1777

answers:

3

Is there a way of finding out which row is current in a TDBGrid?

+2  A: 

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.

Kluge
+1  A: 

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.

mghie
+2  A: 

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.

JosephStyons