Forum > Databases

Looking up clicked-on values from a DBGrid

(1/1)

heebiejeebies:
Hi all,

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


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---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!

dseligo:

--- Quote from: heebiejeebies 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  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---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!

--- End quote ---

In your query 'select' value you want to show in DBGrid, but also primary index.
Something like that:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---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:
Thanks very much, it seems to be working finally!  :)

Navigation

[0] Message Index

Go to full version