DBGrid - disable first record selection after query open lazarus / 16-Databases ---------------------------------------------------------------------- Dibo · 2014-11-15 21:14 Hi, I have problem with TSqlQuery, TDataSource and TDBrid. TSqlQuery after open always scroll to the first record and it is selected in DBGrid. TSqlQuery.Open also trigger TSqlQuery.AfterScroll event. What I want to do is: 1. Open query 2. Nothing is selected in DBGrid 3. AfterScroll event is not called 4. When user select record then AfterScoll is called The reason is that on AfterScoll event I load a lot of data in background. So when user open form, query is opening and form is freezing because query is scrolling to the first record and all other stuff is called. I want to show form as soon as possible (with list without selected record) and first record may be not the one which user want to select. Even if I disable AfterScroll event while opening query, grid is still pointing on first record and it can't be "reselected" again for AfterScroll call For example, in Qt, QSqlQuery is always EOF after open. QSqlQuery.Next (or select on grid) move it to the record. ---------------------------------------------------------------------- Mike.Cornflake · 2014-11-16 04:38 I don't think you can stop DBGrid from selecting a record, but I think you can solve this issue.  There are two related calls that are designed to stop the UI from updating while you're processing datasets... Code: [Select]  MyDataSet.DisableControls;   Try     //   Finally     MyDataSet.EnableControls;   End; The only thing I've never tried before is calling .Open on the dataset while controls are disabled, but I can't see this being an issue. Unrelated to your issue, but for information only.   People who write controls are supposed to check if .ControlsEnabled before doing anything with the dataset.  So long as the controls on your form obey this (and DBGRid does) you should be fine. Good luck. UPDATE:  Hmmm.  Thinking about this, you *may* need to stick the following in your .AfterScroll;  If you look at this sideways then your form is essentially a large component, which means your own code needs to obey the ControlsDisabled rules... Code: [Select]  If MyDataSet.ControlsEnabled Then   Begin     //    End; ---------------------------------------------------------------------- taazz · 2014-11-16 04:57 Quote from: Mike.Cornflake on November 16, 2014, 05:38:38 amI don't think you can stop DBGrid from selecting a record, but I think you can solve this issue.  There are two related calls that are designed to stop the UI from updating while you're processing datasets... actually it makes no sense a record is always selected, the data cursor can not be pointing to nothing. Quote from: Mike.Cornflake on November 16, 2014, 05:38:38 amCode: [Select]  MyDataSet.DisableControls;   Try     //   Finally     MyDataSet.EnableControls;   End; The only thing I've never tried before is calling .Open on the dataset while controls are disabled, but I can't see this being an issue. Unrelated to your issue, but for information only.   People who write controls are supposed to check if .ControlsEnabled before doing anything with the dataset.  So long as the controls on your form obey this (and DBGRid does) you should be fine. Good luck. UPDATE:  Hmmm.  Thinking about this, you *may* need to stick the following in your .AfterScroll; Code: [Select]  If MyDataSet.ControlsEnabled Then   Begin     //    End;Calling the DisableControls method will instruct the dataset to ignore all the attached controls and never call / raise events that will make them react to changes, minimizing the execution time of all the jobs that access the dataset, by calling EnableControls a single event is raised that forces all attached controls to be refreshed. In short there is no way to have a dataset that has no data/row selected only a grid that ignores it and doesn't show it but if disablecontrols has been called then I think there shouldn't be any afterscroll events raised that would be against the reason DisableControls exists. ---------------------------------------------------------------------- Mike.Cornflake · 2014-11-16 06:36 Quote from: taazz on November 16, 2014, 05:57:49 am it but if disablecontrols has been called then I think there shouldn't be any afterscroll events raised that would be against the reason DisableControls exists.  I'm not so sure.  I don't think DisableControls is about disabling the scroll events, but about disabling the UI's response to the scroll (and other) events. So sure, if you have DisableControls then the grid isn't going to be issuing an OnScroll, cause the grid will be ignoring all calls.  However, if you specifically call Dataset.Next while Not Dataset.ControlsEnabled, then I would still expect the OnScroll event to fire.  Oops.  Just checked the docs (which I should have done first time) http://www.freepascal.org/docs- html/fcl/db/tdataset.enablecontrols.html QuoteEnableControls tells the dataset to resume sending data-related events to the controls. Looks like you're right @Taazz :-)   I'll leave this post here in case others had the same impression as me... ---------------------------------------------------------------------- snorkel · 2014-11-17 18:12 many controls also have  beginupdate and endupdate which you can use for this purpose. I believe dbgrid has beginupdate and endupdate. I call these in conjunction with a try finally.