Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding the current row in Delphi's TDBGrid

Tags:

delphi

tdbgrid

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

like image 425
BubbaT Avatar asked Mar 23 '09 01:03

BubbaT


2 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.

like image 146
Kluge Avatar answered Sep 19 '22 23:09

Kluge


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.

like image 24
JosephStyons Avatar answered Sep 19 '22 23:09

JosephStyons