Lazarus

Programming => General => Topic started by: madref on March 15, 2019, 09:25:55 pm

Title: [SOLVED] Behavior of DB_Grid
Post by: madref on March 15, 2019, 09:25:55 pm
I have this DB_Grid that shows some values.
See Screen 1.


When I am deleting an entry it shows all the columns in that grid.
See Screen 2


How to avoid/correct this?
Title: Re: Behavior of DB_Grid
Post by: valdir.marcos on March 15, 2019, 11:56:45 pm
I have this DB_Grid that shows some values.
See Screen 1.

When I am deleting an entry it shows all the columns in that grid.
See Screen 2
See the attached small example.
I believe your problem is related to DBGrids.pas:

Code: Pascal  [Select][+][-]
  1. procedure TCustomDBGrid.AddAutomaticColumns;
  2. var
  3.   i: Integer;
  4.   F: TField;
  5. begin
  6.   // add as many columns as there are fields in the dataset
  7.   // do this only at runtime.
  8.   ...
  9. end;
Quote
How to avoid/correct this?
Why shoudn't you expect AddAutomaticColumns to be performed on runtime on your case?
Title: Re: Behavior of DB_Grid
Post by: madref on March 16, 2019, 01:01:35 am
I know that I have to set the width of every column.
I use this procedure for that:
Code: Pascal  [Select][+][-]
  1. procedure TForm_Evaluatie_2018_Linesman.Format_Grid_Observaties_Zwakte;
  2. begin
  3.   if not DBGrid_Observaties_Zwakte.DataSource.DataSet.Active then exit;
  4.   DBGrid_Observaties_Zwakte._ColumnByName('EOS_ID').Width := 0;
  5.   DBGrid_Observaties_Zwakte._ColumnByName('EOS_Wie').Width := 0;
  6.   DBGrid_Observaties_Zwakte._ColumnByName('EOS_EVO_ID').Width := 0;
  7.   DBGrid_Observaties_Zwakte._ColumnByName('Cat').Width := 35;
  8.   DBGrid_Observaties_Zwakte._ColumnByName('Competence').Width := 450;
  9.   DBGrid_Observaties_Zwakte._ColumnByName('EOS_EC_ID').Width := 0;
  10.   TQ_Observaties_Zwakte.Fields[3].Alignment := taCenter;
  11. end;     // Format_Grid_Observaties_Zwakte  
But happens when I delete a record from the database.
Is it possible to 'suspend' the view until all is deleted and refreshed?
Title: Re: Behavior of DB_Grid
Post by: jesusr on March 16, 2019, 03:40:13 am
As exposed it sounds like a bug, columns with width=0 should be respected on record deletion, but there had been some changes there and bugs may have been introduced.

Instead of width=0 have your tried to set the visible property to false?
Title: Re: Behavior of DB_Grid
Post by: valdir.marcos on March 16, 2019, 04:56:06 am
As exposed it sounds like a bug, columns with width=0 should be respected on record deletion, but there had been some changes there and bugs may have been introduced.
Instead of width=0 have your tried to set the visible property to false?
The problem only happens on setting all properties by code (see old attached example).
If everithing is set by Object Inspector (see new attached example), there is no problem.
Title: Re: Behavior of DB_Grid
Post by: jesusr on March 16, 2019, 07:17:39 am
This is a different problem.
By setting the properties at runtime, it's the same as if some user had interactively changed the properties, those properties are not stored anywhere so, if you close and reopen the query you must redo, recreate or replay the actions that changed the grid properties, in your sample you have to copy the third section of your formshow procedure into a new procedure 'UpdateGrid' for example and call it, then after reopened the dataset call 'UpdateGrid' again. I have not checked your second sample but if the properties are stored at design time, then when you reopen the dataset, the grid already have the changed properties (with respect to a unmodified at design time grid) so it should work OK.

The problem madref reported, as I understood it, is that if you delete one record, then some columns that had 0 width (and as a consequence remained hidden), will suddenly become visible because their widths are no 0 anymore.
Title: Re: Behavior of DB_Grid
Post by: valdir.marcos on March 16, 2019, 07:36:54 am
This is a different problem.
By setting the properties at runtime, it's the same as if some user had interactively changed the properties, those properties are not stored anywhere so, if you close and reopen the query you must redo, recreate or replay the actions that changed the grid properties, in your sample you have to copy the third section of your formshow procedure into a new procedure 'UpdateGrid' for example and call it, then after reopened the dataset call 'UpdateGrid' again. I have not checked your second sample but if the properties are stored at design time, then when you reopen the dataset, the grid already have the changed properties (with respect to a unmodified at design time grid) so it should work OK.

The problem madref reported, as I understood it, is that if you delete one record, then some columns that had 0 width (and as a consequence remained hidden), will suddenly become visible because their widths are no 0 anymore.
Madref didn't show a sample, but I believe he is setting the properties at runtime... and there isn't any properties stored anywhere...
If I am right, there is no bug.
Title: Re: Behavior of DB_Grid
Post by: madref on March 16, 2019, 05:01:22 pm
I set the width of each column at runtime so it's true nothing is stored.
Thus I need to set everything back each time I change something.
To avoid everything being seen on  the screen I now simply just enable the DBGrid, do my thing en the able it again.
In code it looks like:
Code: Pascal  [Select][+][-]
  1.     DBGrid_Observaties_Sterkte.Visible := False;
  2.     cSQL := 'DELETE FROM tbl_Evaluatie_Observaties WHERE EOS_ID = ' + Save_ID;
  3.     TQ_Observaties_Sterkte.Active := False;
  4.     TQ_Observaties_Sterkte.DeleteSQL.Text := cSQL;
  5.     TQ_Observaties_Sterkte.Active := True;
  6.     TQ_Observaties_Sterkte.Delete;
  7.     TQ_Observaties_Sterkte.ApplyUpdates;
  8.     if Form_RefereeMain.Trans_RefereeDB.Active then
  9.       Form_RefereeMain.Trans_RefereeDB.CommitRetaining;
  10.     TQ_Observaties_Sterkte.Refresh;
  11.     TQ_Observaties_Sterkte.First;
  12.     Format_Grid_Observaties_Sterkte;
  13.     DBGrid_Observaties_Sterkte.Visible := True;
It's not pretty, but it works :)
Title: Re: Behavior of DB_Grid
Post by: lucamar on March 16, 2019, 05:21:31 pm
Just curious (I don't know that much about TDBGrid), why don't you use BeginUpdate / EndUpdate? Aren't they intended exactly for this kind of situations? All grids (at least all descending from TCustomGrid) should respond to them.

Try somethig like this:

Code: Pascal  [Select][+][-]
  1.   DBGrid_Observaties_Sterkte.BeginUpdate;
  2.   try
  3.     cSQL := 'DELETE FROM tbl_Evaluatie_Observaties WHERE EOS_ID = ' + Save_ID;
  4.     TQ_Observaties_Sterkte.Active := False;
  5.     TQ_Observaties_Sterkte.DeleteSQL.Text := cSQL;
  6.     TQ_Observaties_Sterkte.Active := True;
  7.     TQ_Observaties_Sterkte.Delete;
  8.     TQ_Observaties_Sterkte.ApplyUpdates;
  9.     if Form_RefereeMain.Trans_RefereeDB.Active then
  10.       Form_RefereeMain.Trans_RefereeDB.CommitRetaining;
  11.     TQ_Observaties_Sterkte.Refresh;
  12.     TQ_Observaties_Sterkte.First;
  13.     Format_Grid_Observaties_Sterkte;
  14.   finally
  15.     DBGrid_Observaties_Sterkte.EndUpdate;
  16.   end;
Title: Re: Behavior of DB_Grid
Post by: madref on March 16, 2019, 08:06:41 pm
Didn't know that. but I will try it :)
Title: Re: Behavior of DB_Grid
Post by: madref on March 17, 2019, 04:21:35 am
I tried it and it works like a charm.
Thanks lucamar
TinyPortal © 2005-2018