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!

Getting Data from a Data Grid 1

Status
Not open for further replies.

Jonah86

Programmer
Dec 11, 2003
152
US
Hi. I posted this a few days ago, but it was deemed off topic, I will try to make this a topical as I can.

I am using a data grid(component in Delphi) to view the contents of a database. I need to be able to get a particular data field out of the grid when the user has selected a row. Say for instance the user selects row 3, I want to be able to use, say, the "name" field of that row elsewhere in the program. I'm just not sure how to do it.

I have no idea how this particular subject was off-topic, I realize that I had a later post in that topic that dealt with MySQL, but why couldn't THAT post be deleted instead of me losing answers to my question, and now costing me quite a bit of time looking for a new answer.

Sorry, I'm just a little irked about the way this site is run.

Thank you for any help you may be able to offer before this get closed. You can also e-mail me if you would like.

jcarpenter@medinaco.org

Thanks!
 
The DB controls such as TDBEDit, TDBGrid and so on show the contents of the current record. (In the case of TDBGrid the contents of several records are displayed but only one of them is the current record).

If you want to get a particular field in the currently selected record then you should use the underlying TField for that field. You should not try to get it from one of the DB controls.

There are several ways you can do this but probably the most maintainable way is to use the FieldByName property.

For example, if your TDataset descendant, here called mensa, had a field called 'name' (which is presumably a string) then you could access the name field of the current record by using something like
Code:
  data := Mensa.FieldByName('name').AsString;
You don't say how you are accessing MySQL but you are probably using TSQLDataset. You could access the field in exactly the same way.

You can also access the field by its position in the table. So if 'name' was the second field in mensa then you could access it by
Code:
  data := Mensa.Fields[1].AsString;
This is only recommended if there is some reason you don't know the name of the field - say you are writing a general purpose piece of code which has to work with lots of different tables.

A third way to access fields is to double click on the TDataset descendant and explicity add the names of the fields. Then you can access the fields like this:
Code:
  data := MensaName.AsString;
Each of the three ways has its merits but for most occasions, the first way is most appropriate.

Andrew


 
Ok, thank you very much for your reply. Now, I tried this method, and am only able to get the record from the first record in the table. No matter which record I have selected, I can only get the data from the first.

I have an TQuery, TDataSource and TTable...the table I added as it is the only place I can find the field by name attribute. I'm generally new to all of this...so I'm sorry for my ignorance.

Thanks again.
 
The TDataSource will be linked to either the TQuery or the TTable.

Is it possible that you are accessing the TField belonging to the component that is not linked to the TDataSource? That would explain why you are always getting the contents of the first record.

If your TDataSource is linked to the TQuery (let's say the TQuery has a name of 'Que') then you will access the current record from Que by
Code:
  data := Que.FieldByName('fieldname').AsString;

Andrew
 
Perfect! Worked like a charm...I was mis-understanding a few things...but now I have it. Thank you so much.
 
If you know the index of column, you can use :

dbgForneced.Columns[0].Field.AsString

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top