[SOLVED] DBGrid - Select first Row on Startup lazarus / 16-Databases ---------------------------------------------------------------------- Zvoni · 2026-03-23 09:51 Hi Folks, quick question: Is there any way (in OI or from code), to automatically select the first Row of a DBGrid, when the Form shows up? Scenario: I have a DBGrid on a Form with everything properly connected (datasource etc.). Works as it should. Add, Update, Delete works as expected. But: On Startup of the Form i'd like to select the first Row automatically, as if the user actually clicked on that Row. Have looked everywhere (DBGrid, DataSource, TSqlQuery). Found nothing Background: I actually have Code that is executed in OnCellClick, (setting a Property from dbgrid.datasource.dataset.FieldByName, and calling further code based on that property) I thought setting the Dataset to first Record, but no cigar (better said: partial cigar) The "Problem" is the Row-Highlighting: it highlights the first row, indicating the first row is selected, when it actually is not. I'm currently moving the Dataset to first Record, and then calling DBGridCellClick(Nil) which just looks wrong to me. Any hints? ---------------------------------------------------------------------- Sieben · 2026-03-23 15:27 Any row-select operations in DBGrid are database operations in the end. So yours might be the only way. Does it work as expected? ---------------------------------------------------------------------- JohnJ · 2026-03-23 15:29 getTables(cmnGrid); cmnGrid.TopRow:=3; s1 := cmnGrid.Cells[0, 3];  s1 has the value of the first column in row 3 ---------------------------------------------------------------------- JohnJ · 2026-03-23 15:34 I just realized that I'm using a TStringGrid, not a DBGrid ---------------------------------------------------------------------- valdir.marcos · 2026-03-25 21:58 Quote from: Zvoni on March 23, 2026, 10:51:09 amHi Folks, quick question: Is there any way (in OI or from code), to automatically select the first Row of a DBGrid, when the Form shows up? Scenario: I have a DBGrid on a Form with everything properly connected (datasource etc.). Works as it should. Add, Update, Delete works as expected. But: On Startup of the Form i'd like to select the first Row automatically, as if the user actually clicked on that Row. Have looked everywhere (DBGrid, DataSource, TSqlQuery). Found nothing Background: I actually have Code that is executed in OnCellClick, (setting a Property from dbgrid.datasource.dataset.FieldByName, and calling further code based on that property) I thought setting the Dataset to first Record, but no cigar (better said: partial cigar) The "Problem" is the Row-Highlighting: it highlights the first row, indicating the first row is selected, when it actually is not. I'm currently moving the Dataset to first Record, and then calling DBGridCellClick(Nil) which just looks wrong to me. Any hints? Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i Dataset1.Di sableControls;...Dataset1.First;TForm1.DBGrid1CellClick(DBGrid1.Column s[0]);Label1.Caption := DBGrid1.Fields[0].Value;Label2.Caption := DBGrid1.DataSource.DataSet.FieldByName('ID').AsString;     ...Dataset1.EnableControls; ---------------------------------------------------------------------- Zvoni · 2026-03-26 07:07 Quote from: valdir.marcos on March 25, 2026, 10:58:00 pmQuote from: Zvoni on March 23, 2026, 10:51:09 amHi Folks, quick question: Is there any way (in OI or from code), to automatically select the first Row of a DBGrid, when the Form shows up? Scenario: I have a DBGrid on a Form with everything properly connected (datasource etc.). Works as it should. Add, Update, Delete works as expected. But: On Startup of the Form i'd like to select the first Row automatically, as if the user actually clicked on that Row. Have looked everywhere (DBGrid, DataSource, TSqlQuery). Found nothing Background: I actually have Code that is executed in OnCellClick, (setting a Property from dbgrid.datasource.dataset.FieldByName, and calling further code based on that property) I thought setting the Dataset to first Record, but no cigar (better said: partial cigar) The "Problem" is the Row-Highlighting: it highlights the first row, indicating the first row is selected, when it actually is not. I'm currently moving the Dataset to first Record, and then calling DBGridCellClick(Nil) which just looks wrong to me. Any hints? Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i Dataset1.Di sableControls;...Dataset1.First;TForm1.DBGrid1CellClick(DBGrid1.Column s[0]);Label1.Caption := DBGrid1.Fields[0].Value;Label2.Caption := DBGrid1.DataSource.DataSet.FieldByName('ID').AsString;     ...Dataset1.EnableControls; The Sender of the CellClick is the Column?? Damn. Never thought of that. Thanks valdir. Was exactly what i was looking for ---------------------------------------------------------------------- valdir.marcos · 2026-03-26 08:05 Quote from: Zvoni on March 26, 2026, 08:07:32 amQuote from: valdir.marcos on March 25, 2026, 10:58:00 pmQuote from: Zvoni on March 23, 2026, 10:51:09 amHi Folks, quick question: Is there any way (in OI or from code), to automatically select the first Row of a DBGrid, when the Form shows up? Scenario: I have a DBGrid on a Form with everything properly connected (datasource etc.). Works as it should. Add, Update, Delete works as expected. But: On Startup of the Form i'd like to select the first Row automatically, as if the user actually clicked on that Row. Have looked everywhere (DBGrid, DataSource, TSqlQuery). Found nothing Background: I actually have Code that is executed in OnCellClick, (setting a Property from dbgrid.datasource.dataset.FieldByName, and calling further code based on that property) I thought setting the Dataset to first Record, but no cigar (better said: partial cigar) The "Problem" is the Row-Highlighting: it highlights the first row, indicating the first row is selected, when it actually is not. I'm currently moving the Dataset to first Record, and then calling DBGridCellClick(Nil) which just looks wrong to me. Any hints? Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i Dataset1.Di sableControls;...Dataset1.First;TForm1.DBGrid1CellClick(DBGrid1.Column s[0]);Label1.Caption := DBGrid1.Fields[0].Value;Label2.Caption := DBGrid1.DataSource.DataSet.FieldByName('ID').AsString;     ...Dataset1.EnableControls; The Sender of the CellClick is the Column?? Damn. Never thought of that. Thanks valdir. Was exactly what i was looking forIt was my pleasure.