Recent

Author Topic: Looking up clicked-on values from a DBGrid  (Read 576 times)

heebiejeebies

  • Full Member
  • ***
  • Posts: 127
Looking up clicked-on values from a DBGrid
« on: April 17, 2022, 11:53:46 pm »
Hi all,

I have a DBGrid that is connected to a TSQLQuery with the following SQL:

Code: Pascal  [Select][+][-]
  1. UserFileLoad.SQL.Text := 'select Issue from Custom1';

So the DBGrid shows only the column 'Issue'.  In the OnCellClick event of the DBGrid, I need to load the value from the corresponding 'ID' (primary key) column, based on which row of the table the user has clicked on.  Is there an easy way to do this?  I seem to be running into issues because the SQL query that populates the DBGrid does not load the ID column.

In your answer, please assume I am stupid, because I am.  :)

Thanks!
Fedora 38/Lazarus 2.2.4- FPC 3.3.1

dseligo

  • Hero Member
  • *****
  • Posts: 1177
Re: Looking up clicked-on values from a DBGrid
« Reply #1 on: April 18, 2022, 02:35:47 am »
Hi all,

I have a DBGrid that is connected to a TSQLQuery with the following SQL:

Code: Pascal  [Select][+][-]
  1. UserFileLoad.SQL.Text := 'select Issue from Custom1';

So the DBGrid shows only the column 'Issue'.  In the OnCellClick event of the DBGrid, I need to load the value from the corresponding 'ID' (primary key) column, based on which row of the table the user has clicked on.  Is there an easy way to do this?  I seem to be running into issues because the SQL query that populates the DBGrid does not load the ID column.

In your answer, please assume I am stupid, because I am.  :)

Thanks!

In your query 'select' value you want to show in DBGrid, but also primary index.
Something like that:
Code: Pascal  [Select][+][-]
  1. UserFileLoad.SQL.Text := 'select primary_key_field_name, Issue from Custom1';

DBGrid without adjusting shows all columns from query. If you want it to show only 'Issue' column then in Object inspector go to editing of Columns property, add new field and in FieldName write 'Issue' (column name that you want to appear in DBGrid).

That's what I've done in this example:
https://forum.lazarus.freepascal.org/index.php/topic,59065.msg440534.html#msg440534

heebiejeebies

  • Full Member
  • ***
  • Posts: 127
Re: Looking up clicked-on values from a DBGrid
« Reply #2 on: April 18, 2022, 08:06:44 am »
Thanks very much, it seems to be working finally!  :)
Fedora 38/Lazarus 2.2.4- FPC 3.3.1

 

TinyPortal © 2005-2018