Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to get the selected record index in a TDBGrid?

Status
Not open for further replies.

davidchardonnet

Programmer
Mar 21, 2001
167
FR
Hello,

I am trying to get the index of the selected row in a TDBGrid linked to a database, so that I can get the selected data in my own variables for an update. But I can't figure out how to do this because the DBGrid1.SelectedRows.Count is always 0, even when I have something selected...
And SelectedRows is always empty...
The DBGrid has the dgRowSelect Option set to true.

Do you know how I can do this?

Thank you very much

David
 
Hi David -

I suggest you forget about the Grid itself and focus on the Dataset to which its bound. For instance, I have a DBGrid that is bound to a query and I attach the following code to the OnCellClick and OnCellEnter events :

CurrCrit.Caption := MediaQry.Fields[0].AsString;


CurrCrit is a label component I use to hold the details of the current record (you could just as easily pass this to whatever you wanted) and MediaQry is the name of the Query that supplies data for the DBGrid. I Use Fields[0] because the first field in the DB is the key field for me.

As the DBGrid is just a mirror to the state of its dataset - this is a valid way to monitor the current record (in the grid itself.)

Of course, the exact code used will vary depepnding on your needs (and components used) but I think it serves as an example.

Opp.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top