View Issue Details

IDProjectCategoryView StatusLast Update
0033281LazarusLCLpublic2018-03-05 13:45
ReporterGiovanni SearaAssigned ToJesus Reyes 
Status closedResolutionfixed 
PlatformDesktopOSWindowsOS Version10
Product Version1.8Product Build 
Target Version1.8.4Fixed in Version1.9 (SVN) 
Summary0033281: When the field is selected in DBGrid, the value is not set.
DescriptionWhen the Field is selected in DBGrid and the DataSet state are editing, setting the value of Field does not change.
Additional InformationFor the tests, the EditButtonClick function of the DBGrid was used and it was verified that the error occurs in two way:

1 - When the Field is selected in the DBGrid and does not have any change in its value, it is not changed by the user, and this value is changed via code via the column button the field value is changed, however visually in the DBGrid the value prior is maintained and be updated only when you leave the field.

2 - When the Field is selected in the DBGrid and has change in value by the user, and after its value is changed by code via the column button, the Field value is changed, however visually in the DBGrid the previous value is maintained and when it exits the field value is replaced by what was visually in the DBGrid.

For a better understanding watch the attached video.
TagsDbCtrls, lcl, TDBGrid
Fixed in Revision57428, 57438
Attached Files


Giovanni Seara

2018-02-28 16:38


Video_1519752603.wmv (853,317 bytes)

Giovanni Seara

2018-02-28 16:39


BUG - Field selected not set (2,973 bytes)


2018-02-28 18:52

developer   ~0106769

Not sure if I understood everything.

I could reproduce this:
- Click some item in column "Letter"
- Click on Edit
- Click that same item again to show the '...' button.
- Click on the '...' button.
- Your click-code writes 'X' into the field of the "Letter" column.
- You expect to see the 'X' immediately in the grid. This does not happen.

I think you are expecting too much of the OnEditButtonClick event. DBGrid just inherits this event from TCustomGrid, it has nothing to do with databases. If you just modify a field value here and don't do anything else, how will the grid get to know that it should redraw the new content?

Here are two solutions for the issue:

* Post the dataset in the OnEditButtonClick

  procedure TForm1.DBGrid1EditButtonClick(Sender: TObject);
    if DBGrid1.SelectedField.Index = 1 then begin
      BufDataset1.Fields[1].AsString := 'X';

* Or put the field change into a DisableControls/EnableControls block which gives the dataset the chance to notify controls of the changed value

  procedure TForm1.DBGrid1EditButtonClick(Sender: TObject);
    if DBGrid1.SelectedField.Index = 1 then begin
        BufDataset1.Fields[1].AsString := 'X';

Giovanni Seara

2018-02-28 20:41

reporter   ~0106771

When a modification to the record through the DataSet occurs, the OnRecordChanged event of the DataLink is fired. So to correct this behavior you must modify the procedure OnRecordChanged of the CustomDBGrid to update the visible value in the DBGrid when there is modification in the DataSet.The error is not related only to the EditButtonClick, but when changing the value of a Field when this Field is selected in the DBGrid.

A possible solution is described below:

procedure TCustomDBGrid.OnRecordChanged (Field: TField);
   c: Integer;
   {$ ifdef dbgDBGrid}
   DbgOut (ClassName, '. OnRecordChanged (Field =');
   if Field = nil then DebugLn ('nil)')
   else DebugLn (Field.FieldName, ')');
   {$ endif}
   if Field = nil then
   else begin
     c: = GetGridColumnFromField (Field);
     if c> 0 then
       if AField = SelectedField then
         SetEditText (Col, Row, AField.Text);
         EditorShowInCell (Col, Row);
       end else
       InvalidateCell (C, Row)

Jesus Reyes

2018-03-01 17:48

developer   ~0106805

I think the problem Giovanni is trying to demonstrate here is why the dbedit "sees" the changed value immediately but the dbgrid doesn't. In fact the grid is probably seeing the same change and the problem is the value is being "obscured" or blocked by the currently visible editor. So my guess is that the editor is just not being updated. I will investigate.

Jesus Reyes

2018-03-02 00:43

developer   ~0106810

Please check.

Giovanni Seara

2018-03-02 14:06

reporter   ~0106816

Hy Jesus,
I performed tests and in this condition the error remains:

When the field is selected in DBGrid and its value is changed by the user when clicking on the column button to modify the value, this value does not change, it set what the user has typed.

Jesus Reyes

2018-03-03 05:32

developer   ~0106825

This is what I do:

1. Run the produced program.
2. Click the cell containing "d" (the cell is now selected)
3. Click the "Edit Button" (the indicator on selected row change to a "|")
4. Again Click the cell containing "d" ("now the [...] button appears)
5. Press the [...] button (the DbEdit now shows "X", the same in the dbGrid Editor, but the "X" appears selected).

Please confirm that:

1. You are using Lazarus trunk with at least revision 57428.
2. You follow the steps I provided.

if the answer is yes for both accounts, please detail your results.


2018-03-03 09:58

developer   ~0106826

Not sure if it is correct to built any anticipated reaction on user code into the combobox.

Try this example:
- Run the demo
- Click in cell "d" -- the cell is selected
- Type anything into the cell -- the DBEdit at the bottom does not show the typed text because it is not yet posted.

- Now restart again
- Follow Jesus' instructions
- At the end, after the click at the ".." button, the X is in the grid, AND it is in the DBEdit --> This looks to me that the record is posted. At least, you cannot press ESC in the grid any more to undo the change.

In my eyes, the cell button editor is something related to user-code. The grid does not know what happens in the user code and cannot - no: must not - assume anything what the user should expect to happen. All these things should happen in the event handler for OnEditButtonClick.

Giovanni Seara

2018-03-03 15:09

reporter   ~0106834

Last edited: 2018-03-03 15:11

View 2 revisions

I'm doing the following steps:

1. Run the program.
2. Click the cell that contains "d" (the cell is now selected)
3. Click the "Edit" button (the indicator on the selected line changes to a "|")
4. Again, click the cell that contains "d" ("now the [...] button appears)
5. press "a" through the keyboard.
6. Press the [...] button (the "a" appears in DBEdit and the value in DBGridEditor not is changed, only selected).

In my view, at this point the value of the DBGridEditor/Field should be "X" and not "a".

Jesus Reyes

2018-03-03 23:25

developer   ~0106843

@wp: when you say " ... because it is not yet posted." do you mean "updated or propagated"?, posted imply that .post has been called. Indeed, typing something in the editor should not update the field and hence propagate the change. However, later you say this "... This looks to me that the record is posted. At least, you cannot press ESC in the grid any more to undo the change." Here you really talk about the effect of .post because you say that ESC will not work anymore. But the user code do not call .post, only updates the field so if post is really called it must be in the LCL code, which should be fixed.

@Giovanni: It means that you can reproduce the steps I gave earlier?, I also think that at the end There should be an "X" and not an "a". I will take a look.


2018-03-04 01:14

developer   ~0106846

Yes, "post" is probably not the right word - I did not look whether Post has been called. "Propagate" is probably correct.

It just wanted to say that it is inconsistent to me that the edit button click "propagates" to change, but a keypress does not. I would have expected that the change due to the button click remains inside the grid.

Jesus Reyes

2018-03-04 02:58

developer   ~0106847

The button shouldn't do anything by itself, if it does (maybe because the edit control detects it is losing the focus) this needs to be checked and corrected. Of course, the user is free to do anything in the click handler.

Jesus Reyes

2018-03-04 09:19

developer   ~0106848

Please try again

Giovanni Seara

2018-03-05 13:45

reporter   ~0106885

I re-run the tests and the bugs have been fixed. Thank you.

Issue History

Date Modified Username Field Change
2018-02-28 16:38 Giovanni Seara New Issue
2018-02-28 16:38 Giovanni Seara File Added: Video_1519752603.wmv
2018-02-28 16:39 Giovanni Seara Tag Attached: DbCtrls
2018-02-28 16:39 Giovanni Seara Tag Attached: lcl
2018-02-28 16:39 Giovanni Seara Tag Attached: TDBGrid
2018-02-28 16:39 Giovanni Seara File Added: BUG - Field selected not set
2018-02-28 18:52 wp Note Added: 0106769
2018-02-28 20:41 Giovanni Seara Note Added: 0106771
2018-03-01 17:30 Jesus Reyes Assigned To => Jesus Reyes
2018-03-01 17:30 Jesus Reyes Status new => assigned
2018-03-01 17:48 Jesus Reyes Note Added: 0106805
2018-03-02 00:43 Jesus Reyes Fixed in Revision => 57428
2018-03-02 00:43 Jesus Reyes LazTarget => 1.8.2
2018-03-02 00:43 Jesus Reyes Note Added: 0106810
2018-03-02 00:43 Jesus Reyes Status assigned => resolved
2018-03-02 00:43 Jesus Reyes Fixed in Version => 1.9 (SVN)
2018-03-02 00:43 Jesus Reyes Resolution open => fixed
2018-03-02 00:43 Jesus Reyes Target Version => 1.8.4
2018-03-02 14:06 Giovanni Seara Note Added: 0106816
2018-03-02 14:06 Giovanni Seara Status resolved => assigned
2018-03-02 14:06 Giovanni Seara Resolution fixed => reopened
2018-03-03 05:32 Jesus Reyes Note Added: 0106825
2018-03-03 05:32 Jesus Reyes Status assigned => feedback
2018-03-03 09:58 wp Note Added: 0106826
2018-03-03 15:09 Giovanni Seara Note Added: 0106834
2018-03-03 15:09 Giovanni Seara Status feedback => assigned
2018-03-03 15:11 Giovanni Seara Note Edited: 0106834 View Revisions
2018-03-03 23:25 Jesus Reyes Note Added: 0106843
2018-03-04 01:14 wp Note Added: 0106846
2018-03-04 02:58 Jesus Reyes Note Added: 0106847
2018-03-04 09:19 Jesus Reyes Fixed in Revision 57428 => 57428, 57438
2018-03-04 09:19 Jesus Reyes Note Added: 0106848
2018-03-04 09:19 Jesus Reyes Status assigned => resolved
2018-03-04 09:19 Jesus Reyes Resolution reopened => fixed
2018-03-05 13:45 Giovanni Seara Note Added: 0106885
2018-03-05 13:45 Giovanni Seara Status resolved => closed