View Issue Details

IDProjectCategoryView StatusLast Update
0033734LazarusLCLpublic2018-05-23 05:51
ReporterPaulo SérgioAssigned ToJesus Reyes 
PrioritynormalSeverityminorReproducibilityalways
Status resolvedResolutionfixed 
PlatformDesktopOSWindowsOS Version10
Product Version1.9 (SVN)Product Build 
Target VersionFixed in Version1.9 (SVN) 
Summary0033734: The DBGrid's field is filled with garbage when the focus is changed to another field.
DescriptionIn the form, I added a BufDataset connected to DBGrid.

My code:

procedure TForm1.FormCreate(Sender: TObject);
begin
  BufDataset1.FieldDefs.Add('Field1', ftString, 10);
  BufDataset1.FieldDefs.Add('Field2', ftString, 10);
  BufDataset1.CreateDataset;

  BufDataset1.AppendRecord(['aaaaaaaaaa', 'bbbbbbbbbb']);
  BufDataset1.AppendRecord(['cccccccccc', 'dddddddddd']);
end;
Steps To ReproduceSteps:
1 - Run the Program.
2 - Click in first field of the first record.
3 - Put the field in edit mode and position the caret at the end of the text.
4 - Type anything.
5 - Click on another field.
6 - The first field is empty. And the previous text is lost.

Another way to cause the bug.

Steps:
1 - Run the Program.
2 - Click in second field of the first record.
3 - Typing, replace text for "12345".
4 - Click in first field of the first record.
5 - Put the field in edit mode and position the caret at the end of the text.
6 - Type anything.
7 - Click on another field.
6 - The text of the first field is changed to "12345". And the previous text is lost.
Additional InformationThe bug occurs because of the field FTempText in the class TCustomDBGrid.

To solve the problem I did this:

function TCustomDBGrid.GetEditText(aCol, aRow: Longint): string;
var
  aField: TField;
begin
  Result := '';
  if FDataLink.Active then begin
    aField := GetFieldFromGridColumn(aCol);
    if aField<>nil then begin
      if CheckDisplayMemo(aField) then
        Result := aField.AsString
      else
        Result := aField.Text;
    end;
  end;
  
  FTempText := Result; //<------- Solve the problem
  
end;


Tested in Lazarus versions: 1.8.0, 1.8.2 and 1.9.0
TagsNo tags attached.
Fixed in Revision58010
LazTarget-
WidgetsetWin32/Win64
Attached Files
  • Bug DBGrid FTempText.zip (2,382 bytes)
  • dbgrids.pas.diff (722 bytes)
    Index: dbgrids.pas
    ===================================================================
    --- dbgrids.pas	(revision 57794)
    +++ dbgrids.pas	(working copy)
    @@ -3433,15 +3482,19 @@
         else
           result := true;  // field is nil so it's readonly
     
    -    EditingColumn(Col, not Result);
    +    //EditingColumn(Col, not Result);
       end;
     end;
     
     procedure TCustomDBGrid.EditorTextChanged(const aCol, aRow: Integer;
       const aText: string);
    +var
    +  isReadOnly: Boolean;
     begin
    -  if not EditorIsReadonly then
    +  isReadOnly := EditorIsReadonly;
    +  if not isReadOnly then
         SetEditText(aCol, aRow, aText);
    +  EditingColumn(Col, not isReadOnly);
     end;
     
     procedure TCustomDBGrid.HeaderSized(IsColumn: Boolean; Index: Integer);
    
    dbgrids.pas.diff (722 bytes)

Activities

Paulo Sérgio

2018-05-14 23:29

reporter  

Bug DBGrid FTempText.zip (2,382 bytes)

Jesus Reyes

2018-05-16 20:28

developer   ~0108348

Thanks for your demo and the explanation, however I cannot reproduce the problem neither the first procedure or the second. What lazarus revision are you using?

Paulo Sérgio

2018-05-17 00:06

reporter   ~0108350

Revision 57947 of Lazarus.

Rather than, "Click on another field."
Move to another record, causing post.

Jesus Reyes

2018-05-17 08:32

developer   ~0108353

Thanks, that helps to reproduce the problem.

Jesus Reyes

2018-05-17 20:28

developer   ~0108376

The problem with the suggested patch is that it merely works as if you had deleted a char and then restored it again, I mean, the record will be updated even if there is no real change. I want to avoid updating the record in this cases where no real change was made. Please try the attached patch (it will not apply cleanly because line numbers will not match as I have some pending changes in dbgrid)

Jesus Reyes

2018-05-17 20:28

developer  

dbgrids.pas.diff (722 bytes)
Index: dbgrids.pas
===================================================================
--- dbgrids.pas	(revision 57794)
+++ dbgrids.pas	(working copy)
@@ -3433,15 +3482,19 @@
     else
       result := true;  // field is nil so it's readonly
 
-    EditingColumn(Col, not Result);
+    //EditingColumn(Col, not Result);
   end;
 end;
 
 procedure TCustomDBGrid.EditorTextChanged(const aCol, aRow: Integer;
   const aText: string);
+var
+  isReadOnly: Boolean;
 begin
-  if not EditorIsReadonly then
+  isReadOnly := EditorIsReadonly;
+  if not isReadOnly then
     SetEditText(aCol, aRow, aText);
+  EditingColumn(Col, not isReadOnly);
 end;
 
 procedure TCustomDBGrid.HeaderSized(IsColumn: Boolean; Index: Integer);
dbgrids.pas.diff (722 bytes)

Paulo Sérgio

2018-05-18 15:51

reporter   ~0108410

Yes, you're right.

The bug was fixed and your patch is working as you wanted it to.

Thank you Jesus Reyes.

Issue History

Date Modified Username Field Change
2018-05-14 23:29 Paulo Sérgio New Issue
2018-05-14 23:29 Paulo Sérgio File Added: Bug DBGrid FTempText.zip
2018-05-15 00:40 Jesus Reyes Assigned To => Jesus Reyes
2018-05-15 00:40 Jesus Reyes Status new => assigned
2018-05-16 20:28 Jesus Reyes LazTarget => -
2018-05-16 20:28 Jesus Reyes Note Added: 0108348
2018-05-16 20:28 Jesus Reyes Status assigned => feedback
2018-05-17 00:06 Paulo Sérgio Note Added: 0108350
2018-05-17 00:06 Paulo Sérgio Status feedback => assigned
2018-05-17 08:32 Jesus Reyes Note Added: 0108353
2018-05-17 20:28 Jesus Reyes Note Added: 0108376
2018-05-17 20:28 Jesus Reyes File Added: dbgrids.pas.diff
2018-05-18 15:51 Paulo Sérgio Note Added: 0108410
2018-05-23 05:50 Jesus Reyes Fixed in Revision => 33734
2018-05-23 05:50 Jesus Reyes Status assigned => resolved
2018-05-23 05:50 Jesus Reyes Fixed in Version => 1.9 (SVN)
2018-05-23 05:50 Jesus Reyes Resolution open => fixed
2018-05-23 05:51 Jesus Reyes Fixed in Revision 33734 => 58010