Recent

Author Topic: [SOLVED] Behavior of DB_Grid  (Read 3068 times)

madref

  • Hero Member
  • *****
  • Posts: 949
  • ..... A day not Laughed is a day wasted !!
    • Nursing With Humour
[SOLVED] Behavior of DB_Grid
« 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?
« Last Edit: March 17, 2019, 04:21:47 am by madref »
You treat a disease, you win, you lose.
You treat a person and I guarantee you, you win, no matter the outcome.

Lazarus 3.99 (rev main_3_99-649-ge13451a5ab) FPC 3.3.1 x86_64-darwin-cocoa
Mac OS X Monterey

valdir.marcos

  • Hero Member
  • *****
  • Posts: 1106
Re: Behavior of DB_Grid
« Reply #1 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?

madref

  • Hero Member
  • *****
  • Posts: 949
  • ..... A day not Laughed is a day wasted !!
    • Nursing With Humour
Re: Behavior of DB_Grid
« Reply #2 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?
You treat a disease, you win, you lose.
You treat a person and I guarantee you, you win, no matter the outcome.

Lazarus 3.99 (rev main_3_99-649-ge13451a5ab) FPC 3.3.1 x86_64-darwin-cocoa
Mac OS X Monterey

jesusr

  • Sr. Member
  • ****
  • Posts: 484
Re: Behavior of DB_Grid
« Reply #3 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?

valdir.marcos

  • Hero Member
  • *****
  • Posts: 1106
Re: Behavior of DB_Grid
« Reply #4 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.

jesusr

  • Sr. Member
  • ****
  • Posts: 484
Re: Behavior of DB_Grid
« Reply #5 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.
« Last Edit: March 16, 2019, 07:22:36 am by jesusr »

valdir.marcos

  • Hero Member
  • *****
  • Posts: 1106
Re: Behavior of DB_Grid
« Reply #6 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.

madref

  • Hero Member
  • *****
  • Posts: 949
  • ..... A day not Laughed is a day wasted !!
    • Nursing With Humour
Re: Behavior of DB_Grid
« Reply #7 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 :)
« Last Edit: March 16, 2019, 05:02:58 pm by madref »
You treat a disease, you win, you lose.
You treat a person and I guarantee you, you win, no matter the outcome.

Lazarus 3.99 (rev main_3_99-649-ge13451a5ab) FPC 3.3.1 x86_64-darwin-cocoa
Mac OS X Monterey

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: Behavior of DB_Grid
« Reply #8 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;
« Last Edit: March 16, 2019, 05:26:23 pm by lucamar »
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

madref

  • Hero Member
  • *****
  • Posts: 949
  • ..... A day not Laughed is a day wasted !!
    • Nursing With Humour
Re: Behavior of DB_Grid
« Reply #9 on: March 16, 2019, 08:06:41 pm »
Didn't know that. but I will try it :)
You treat a disease, you win, you lose.
You treat a person and I guarantee you, you win, no matter the outcome.

Lazarus 3.99 (rev main_3_99-649-ge13451a5ab) FPC 3.3.1 x86_64-darwin-cocoa
Mac OS X Monterey

madref

  • Hero Member
  • *****
  • Posts: 949
  • ..... A day not Laughed is a day wasted !!
    • Nursing With Humour
Re: Behavior of DB_Grid
« Reply #10 on: March 17, 2019, 04:21:35 am »
I tried it and it works like a charm.
Thanks lucamar
You treat a disease, you win, you lose.
You treat a person and I guarantee you, you win, no matter the outcome.

Lazarus 3.99 (rev main_3_99-649-ge13451a5ab) FPC 3.3.1 x86_64-darwin-cocoa
Mac OS X Monterey

 

TinyPortal © 2005-2018