Get selected line in dbgrid

cypherbad
cypherbad used Ask the Experts™
on
Hi experts,
I work with dbgrids and i need to get the value of the first row on the selected line. How can i do this?

Thanks.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Commented:
Your DBGrid should have a Datasource property, that Datasource should have a DataSet property
Your DBGrid represents the information in that Dataset.
All you need to do is say MyDataset.Fieldbyname('FieldName').AsString;

Author

Commented:
thanks for the quick answer, i'll test it right away!

Commented:
There is also a way to refer to the first column by number instead of by name, try something like this:
  MyDataset.Fields[0].AsString;

Here's another trick. You can refer to your dataset indirectly by going through the DBGrid:

DBGrid1.Datasource.Dataset.Fields[0].AsString;

Note the 'AsString' part - you have other options like .value, .AsInteger, .AsFloat, .AsCurrency, .AsBoolean etc
Become a Microsoft Certified Solutions Expert

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

Author

Commented:
Please open this: http://i32.tinypic.com/2wbzuqu.jpg
Examples: If line x is selected then i need the value of the field with coordinates (1,x)
If line y is selected i need the value on the field (1,y)

Also on my dbgrid1.datasource.dataset. there is not 'fieldbyname'

Thanks.

Commented:
Well we'll need to know what dataset you're using. Follow the process:
Click on your DBGrid. In the Object Inspector check what the value of the property  Datasource is, for example it might be DataSource1.
Find and select DataSource1.
In the Object Inspector, check for the value of the property under 'Dataset', for example ADOTable1.
Find and select ADOTable1.
In the Object Inspector, at the top it should say something like:
ADOTable1 TADOTable
The information I'm looking for is 'TADOTable' - the Type of the Object. In the case of TADOTable I know you're using an ADODataset

Author

Commented:
I am using as dataset on my datasource a adoquery.
My dbgrid is linked to a datasource that is linked to a adoquery

Commented:
TADOQuery does have a FieldByName property. Please check again

Author

Commented:
indeed. Thanks a lot..

Author

Commented:
sorry for being dumb xD

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial