TDbf cursor not in sync with DBGrid and DBNavigator lazarus / 16-Databases ---------------------------------------------------------------------- tatamata · 2023-02-10 08:59 DBGrid connected via Datasource to a TDBF dataset (table level 7). I can normally navigate and insert/edit data via DbGrid. But when I try to edit a row programmatically (simple Edit/Post), value is always edited in the first record, although I am positioned in a different rows in DBGrid. It is like TDBF dataset and DBGrid are not positioned on the same row, how can it be? What could be the reason? ---------------------------------------------------------------------- wp · 2023-02-10 09:48 Cannot reproduce (Win 11) - see attached project. Please post a simple project showing the issue. ---------------------------------------------------------------------- tatamata · 2023-02-10 18:44 Is it normal that Dataset.Edit puts dataset into dsInsert state? It seem that my tdbf dataset is in both dsInsert and dsEdit states after Dataset.Edit - is this normal? ---------------------------------------------------------------------- wp · 2023-02-10 18:58 Again, I cannot reproduce this. Modify above sample as follows: 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 uses  TypInfo; procedure TForm1.Button3Click(Sender: TObject);begin  Dbf1.Edit;  Caption := GetEnumName(TypeInfo(TDatasetState), integer(Dbf1.State));  Dbf1.FieldByName('TextField').AsString := 'abc';  Dbf1.Post;end; procedure TForm1.Button1Click(Sender: TObject);begin  Dbf1.Edit;  Caption := GetEnumName(TypeInfo(TDatasetState), integer(Dbf1.State));  Dbf1.FieldByName('IntField').AsInteger := 999;  Dbf1.Post;end; This code writes the Dbf1.State into the form's Caption after calling Dbf1.Edit. -- In each case, the Caption displays "dsEdit", as expected. Quote from: tatamata on February 10, 2023, 07:44:39 pmIt seem that my tdbf dataset is in both dsInsert and dsEdit states after Dataset.Edit - is this normal? This is not possible. TDatasetState is an enumeration, not a set. Therefore it can be either dsEdit, or dsInsert, or some other value of the enumeration, but not two values at the same time. 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 type  TDataSetState = (dsInactive, dsBrowse, dsEdit, dsInsert, dsSetKey,    dsCalcFields, dsFilter, dsNewValue, dsOldValue, dsCurValue, dsBlockRead,    dsInternalCalc, dsOpening, dsRefreshFields); ---------------------------------------------------------------------- tatamata · 2023-02-10 19:36 You are right, it is only one state shown in the Caption, however it is not dsEdit, but dsInsert! For example, OnNewRecord event code is triggered as consequence... I don't know, I don't see anything obvious what could cause it...strange. ---------------------------------------------------------------------- Sieben · 2023-02-10 20:05 Calling Edit on an empty dataset results in actually Append being called. Are you sure to edit the right dataset? ---------------------------------------------------------------------- tatamata · 2023-02-11 11:17 I need to correct myself. If Storage=stoMemory, then on TDBF.Edit it goes into dsInsert, while if Storage=stoFile then it goes into dsEdit, but edit happens always on the first row, although in DBGrid cursor is positioned on some other record, let's say record no 4. After Post, if I click refresh in DBNavigator, in DBGrid it is still on record no 4, but edit done on record 1 (done programmatically) becomes visible. Furtherly, I realized that even before I am doing Edit programatically, dataset is positioned on first record all the time, although in Datagrid I am positioned on some other record. Manual editing through DBGrid, however, works normally and edits are saved in proper record. It seems like cursor in DBGrid is different than in TDBF dataset.... ---------------------------------------------------------------------- tatamata · 2023-02-11 21:16 Not only that, but if I inspect in which row TDBF dataset cursor is positioned, while browsing in DBGrid, DBFDataset remains positioned in first row all the time, irrespective of navigating through DBGrid or DBNavigator. Now comes the interesting part. The TBFDataset and Datasource were originally placed in a DataModule. If I copy the DBFDataset and Datasource to the form and connect datagrid to that datasource placed on the form, now it works properly! So I am concluding that the problem is due to DBfDataset and Datasource being placed in a datamodule, not in the form where DBGrid is placed! Does it make sense? Is it possible that DBFDataset and Datasource must be placed in the form where controls are using it, instead of data module? ---------------------------------------------------------------------- wp · 2023-02-11 21:56 I rewrote the project of reply #1 so that the DBF component is placed on a Datamodule. I also added an OnIdle handler which displays which DBF record is active at any time. There is nothing extraordinary occuring in this application. You should take the time to set up a small project and upload it here so that everybody can see the issue. ---------------------------------------------------------------------- tatamata · 2023-02-11 22:05 Thank you wp for checking this! I have found what was wrong - it is an old program that I wrote ten years ago and now wanted it to resurrect. I saw that Datamodule is not included in auto created forms under project options, so I though it was some mistake from past and included it into auto created forms. I totally forgot that it is manually created through code in the main form's OnCreate event  Now, when I removed data module from auto created forms, everything works OK. I am sorry for false alarm and wasting your time... ---------------------------------------------------------------------- wp · 2023-02-11 22:17 No problem. Good to know there's no such ugly bug hidden somewhere in TDbf. ---------------------------------------------------------------------- egsuh · 2023-02-13 12:54 Haha so your program was pointing to different dataset. It happens. ---------------------------------------------------------------------- tatamata · 2023-02-14 06:58 Ok, for those who are curious, it was MightyQuery program that I was refactoring. Source code (CodeTyphon v. 8.00): https://matasoft.hr/MightyQuery.7z Compiled binaries for Linux and Windows: https://matasoft.hr/MightyQuery_Binary.7z