Is there a way of finding out which row is current in a TDBGrid?
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With