Access to TDBGrid Cell data lazarus / 31-LCL ---------------------------------------------------------------------- iru · 2011-07-27 04:03 Gentlefolk, I have a program with a DBGrid linked to a Firebird database via ZEOS and uses persistent fields. The program processes the results of athletics performances and requires extensive data validation before the data is written to the database. Input data is written to the Dataset fields when the cell is changed. I need to get to the data in the current DBGrid "cell" with control in the cell, for instance on on pressing the 'backspace' key I need to access the "cell" data. I believe that the "cell" input data may be in a string "FTempText" in DBGrid. I have tried typecasting DBGrid to  access FTempText with no success. Does anyone have ANY idas on how I can access the current cell input data? Thanks, Ian. ---------------------------------------------------------------------- Zoran · 2011-07-27 06:47 Quote from: iru on July 27, 2011, 06:03:25 amDoes anyone have ANY idas on how I can access the current cell input data? I don't have any. However, it seems to me that you could use some of the following approaches instead:  1. The persistent field you want to set has OnValidate event. Try to examine YourField.AsString here and call Abort if the field's value is not correct. 2. You can try using the DataSet's (Query's) BeforePost event, examine the field here and call Abort if needed. 3. The field has EditMask property. See how to format the mask here: h ttp://docs.embarcadero.com/products/rad_studio/delphiAndcpp2009/HelpUp date2/EN/html/delphivclwin32/MaskUtils_TEditMask.html ---------------------------------------------------------------------- iru · 2011-07-27 12:06 Thank you for the response. I have worked through what you suggested. 1. The OnValidate occurs when data is written to the dataset. This only happens when I have valid data and write it to the dataset (Dataset.FieldByName().AsInteger := 123). I attempted to modify the error code to write the bad data to the dataset field. (Dataset.FieldByName().AsInteger := ??,  Where do I get ?? from?). 2. The "BeforePost". I modified the error code by adding a Dataset.Post. When an error occured. The "BeforePost" event occurs and the "SetText(Sender: TField; const aText: string) "aText" supplied the bad input data. 3. EditMask. Given the number of different input fields/formats this is not an option. Therefore number 2 appears the way to go. I however need to resolve what appear to be xome side effects..... Thanks again, iru. ---------------------------------------------------------------------- iru · 2011-07-28 04:27 Gentlefolk, The side effects of placing a Dataset.post in the error code are interesting. The data is written to the database. The 'error' data is available at the 'BeforePost'. However in the DBGrid the error input is NOT displayed and the 'current' cell is changed to the first ROW of the grid (same column). Not good...... An example of one type of checking/control needed and my logic is as follows: Input an athletes registration number. Check if this exists in a local database. If found load the athletes data (name/s, date of birth, club, etc) into the Dataset.Results and display the data in the associated DBGrid, focus on the next cell in the same column - all works OK. If NOT found inform the user (beep, message in status bar) hold the focus in the same cell with the unknown athletes registration number displayed. At this point the user will edit the bad registration by backspace, DEL, etc. This is where I run into trouble because to provide access to the input before doing a post or anything like that I have been accumulating a string of input in parallel with the data accumulated by the DBgrid. The background of the cell is highlighted and a BackSpace will delete all input in the DBGrid. Unfortunately in my code on the same BackSpace I cannot determine if the cell background is highlighted and therefore cannot know whether to 'BackSpace' one or all characters. I think I need a rethink.......... iru. ---------------------------------------------------------------------- iru · 2011-07-28 05:53 Gentlefolk, Some more on this one. Modified the code as follows: On 'good' input (valid athlete number in the REG column) move all other fields to the dataset (dataset.FieldByname().As.....) for each required field. The 'BeforeEvent' event for the 'REG' field in the associated Dataset occurs. A line of code in the BeforeEvent loads the 'a.Text' to the ZQuery.FieldByname...... The 'Validate' event on the 'REG' field occurs and I can access the 'REG' data again. More interestingly, I have nothing like an 'ApplyUpdates' or Post and the focus is pqassed to the following row (as required). More work...... Ian. ---------------------------------------------------------------------- Ignatus · 2011-09-15 19:05 I've solved validation of data posted to DB for myself. It might be done by setting the OnSetText handler of the field. See this, double field handler: Code: [Select]procedure TMW.SetDblField(Sender: TField; const AText: String); var d:double;c:byte; begin    val(AText,d,c);    if c=0 then Sender.AsFloat:=d end; procedure TMW.FormCreate(Sender: TObject); begin    MemDataSet1.AppendRecord(['centimeter',10.0]);    MemDataSet1.AppendRecord(['inch',25.4]);    MemDataSet1.Fields[1].OnSetText:= @SetDblField; end;