[SOLVED] select field in dbgrid lazarus / 61-Beginners ---------------------------------------------------------------------- scons · 2017-04-22 08:16 Hi, I have an SDFDataset linked to a DBGrid, I want to be able to select a field (by code) in the DBGrid corresponding to an input from a string. I now have this but the field in the DBGrid does not get selected: 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             if SdfDataSet1.FieldByName('ID').AsString = BList[0] then             begin                  DBGrid1.SelectedField.AsString:= BList[0];                DBGrid1.SetFocus;               end;  What is wrong with this ? Thanks ---------------------------------------------------------------------- wp · 2017-04-22 08:41 I assume you want to select the field which is called 'ID' if it has some given value. Your code changes the value of the selected field, but does not select the field. Better try this: 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 var  F: TField;begin  F := SdfDataset1.FieldByName('ID');  if F.AsString = BList[0] then   begin    DBGrid1.SelectedField := F;    DBGrid1.SetFocus;  end; ---------------------------------------------------------------------- scons · 2017-04-22 09:20 Okay, I see my mistake. I tried your code but it does not seem to find the corresponding value in the DBGrid. If I do this: 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           writeln(BList[0]);            F := SdfDataset1.FieldByName('ID');          if F.AsString = BList[0] then            begin              writeln(BList[0]);              DBGrid1.SelectedField := F;            DBGrid1.SetFocus;            end;  the first writeln writes the value from BList[0], the second does not write, so that makes me assume it can not find the value ? The SdfDataset is a csv file i read, the DBGrid gets filled automatically. 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    SdfDataSet1.FileName := file;  SdfDataSet1.Open;  Datasource1.DataSet := SdfDataSet1;  DBGrid Datasource = Datasource1 (in Object inspector) ---------------------------------------------------------------------- wp · 2017-04-22 10:57 How is the field 'ID' declared? Maybe it is stored as a widestring and ? Try F := FieldbyName('ID').AsWideString. Does it contain non-ASCII characters? Then you must take care of the string's encoding. What you see on the screen after WriteLn and what the comparison of strings in the program see may be different things. If it is an integer field type use AsInteger or similar, try to avoid strings. ---------------------------------------------------------------------- scons · 2017-04-22 12:23 hmm I have to correct myself, I think your code works. Some further investigation shows the selected field jumps to the correct field according if the value matches. For example: if I select a field in row 5 column 6, then run through your code, the field from the code gets selected in the same row (because of the match, in my case column 2). So that works perfectly. My assumption was that it would look through the whole column (2) to look for a match. So if am on row 7 and run through the code, it can not find a match so the selection stays on row 5 col 2. It does not search in the whole DBGrid column to look for the correct match and jump to there. I will have to look for some code which does the search on the whole column and implenetd that with your code. Sorry for being not clear, I had a wrong thought ona possible solution. ---------------------------------------------------------------------- wp · 2017-04-22 12:40 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 begin  if SdfDataset1.Locate('ID', BList[0], []) then begin    DBGrid1.SelectedField := SdfDataset1.FieldByName('ID');    DBGrid1.SetFocus;  end else    ShowMessage('Record not found'); ---------------------------------------------------------------------- scons · 2017-04-24 08:40 It can not seem to find the Field, is there some way I can check this ? (if the application can find the field ?) ---------------------------------------------------------------------- wp · 2017-04-24 08:47 Run the LazDataDesktop application (in folder tools/lazdatadesktop) of your Lazarus installation to open the dbf  file and to display all records in a DBGrid. Now you can look by yourself whether there is any record containing a field ID with the value given by BList[0]. ---------------------------------------------------------------------- scons · 2017-04-24 08:57 Yes it is there, but I read (load) a .csv file, not a dbf. The first line is also used as header for the DBGrid. Column IDB are all unique nrs, ID are all unique nrs. (by it's value, not encoded as field attribute) Code: Text  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i IDB,ID,HPsn r,PosA,Profile,...1,125335,1A,10002,PL8x500,...2,125345,2,10010,PR... ---------------------------------------------------------------------- wp · 2017-04-24 09:35 Ah. I don't know why I got the impression that you are talking of dbf although sdfdataset is clearly mentioned... I looked into the sources of TSdfDataset and it looks that it does not implement its own Locate method, the inherited one does not do anything... So you must go the "painful" way and iterate through all records and look by yourself: 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  variants; function Locate(ADataset: TDataset; const KeyField: string; const KeyValue: Variant;  Options: TLocateOptions): boolean;var  F: TField;begin  Result := false;  F := ADataset.FieldByName(KeyField);  ADataset.DisableControls;  try    ADataset.First;    while not ADataset.Eof do begin      if VarSameValue(F.Value, KeyValue) then       exit(true);      ADataset.Next;    end;  finally    ADataset.EnableControls;  end;end; Please note that this is untested code; if the grid does not highlight the found record comment the lines with DisableControls and EnableControls - but then the grid will follow the seeking process, and it will be quite slow - I hope your table is not too large... To get better support for Locate you should move to TBufDataset or TMemDataset (or sqlite3, but you'll need an external dll here). ---------------------------------------------------------------------- scons · 2017-04-24 20:01 Thanks for your help Mr. wp !! This works ! And I will definately look into your suggestion for moving to sqlite3. But all this is still very new to me (this is my first attemp at a db application) this will take me some time. But I have to admit, with every new thing I learn, I see bit by bit how powerfull this fpc + Lazarus is .