Recent

Author Topic: Weird Problems with TBufDataSet  (Read 1741 times)

LeoBruno

  • Jr. Member
  • **
  • Posts: 64
Weird Problems with TBufDataSet
« on: September 03, 2022, 12:38:10 am »
Hi Guys:

I'm Having weird problems with TBufDataset.

1. A Deleted record continues to show at a DBGrid, even thow the DataSet.IsEmpty returns true.

2. A Filtering with a string that should not retun anything, results on a correctly DataSet.IsEmpty, but one record still shows up at a DBGrid.

Any clues?

10x.
Lazarus 4.4 FPC 3.2.2 Windows - MetaDarkStyle

lainz

  • Hero Member
  • *****
  • Posts: 4743
  • Web, Desktop & Android developer
    • https://lainz.github.io/
Re: Weird Problems with TBufDataSet
« Reply #1 on: September 03, 2022, 12:47:43 am »
In my previous experience with TBufDataset DataSet.IsEmpty is buggy.

LeoBruno

  • Jr. Member
  • **
  • Posts: 64
Re: Weird Problems with TBufDataSet
« Reply #2 on: September 03, 2022, 01:53:35 pm »
The issue here is not DataSet.IsEmpty:

Code: Pascal  [Select][+][-]
  1. procedure TViewNovaEntidadeRelacional.DeleteFK;
  2. begin
  3.   if not mtFKs.IsEmpty then
  4.   begin
  5.     mtFKs.Delete;
  6.   end;
  7. end;
  8.  

There is one record in the Dataset.

When calling the method above, as IsEmpty = false,  mtFKs.Delete Is Called.
But the DGBrid continues to show the record.

Calling the method again, doesn´t do anything, because now, DataSet.IsEmpty = true.

Code: Pascal  [Select][+][-]
  1. procedure TViewNovaEntidadeRelacionalNovaFK.FiltraMtDestino;
  2. begin
  3.   mtColunasDestino.Filtered := false;
  4.   mtColunasDestino.Filter := '';
  5.  
  6.   if Trim(edtFiltro.Text) <> '' then
  7.   begin
  8.     mtColunasDestino.Filter := 'NomeCampo = ' + QuotedStr('*' + edtFiltro.Text + '*');
  9.     mtColunasDestino.Filtered := true;
  10.   end;
  11. end;  
  12.  

If I type X on the edtFiltro, It should result an Empty DataSet, because there is no record with the "X" character on the "NomeCampo" field, but the DBGrid shows the first record of the dataset and DataSet.IsEmpty = true.

So.....

I don´t think that the problem is DataSet.IsEmpty.



In my previous experience with TBufDataset DataSet.IsEmpty is buggy.
« Last Edit: September 03, 2022, 01:55:31 pm by LeoBruno »
Lazarus 4.4 FPC 3.2.2 Windows - MetaDarkStyle

Thaddy

  • Hero Member
  • *****
  • Posts: 19238
  • Glad to be alive.
Re: Weird Problems with TBufDataSet
« Reply #3 on: September 03, 2022, 03:26:27 pm »
Code: Pascal  [Select][+][-]
  1. DBGrid.BeginUpdate;
  2. // dataset code
  3. DBGrid.EndUpdate;
or something similar using one of the events.
objects are fine constructs. You can even initialize them with constructors.

balazsszekely

  • Guest
Re: Weird Problems with TBufDataSet
« Reply #4 on: September 03, 2022, 04:18:20 pm »
Most likely a syncing problem inside TBufDataset, not related to the grid. Please try to refresh the BufDataset after filtering.
Code: Pascal  [Select][+][-]
  1. procedure TViewNovaEntidadeRelacionalNovaFK.FiltraMtDestino;
  2. begin
  3.   mtColunasDestino.Filtered := false;
  4.   mtColunasDestino.Filter := '';
  5.  
  6.   if Trim(edtFiltro.Text) <> '' then
  7.   begin
  8.     mtColunasDestino.Filter := 'NomeCampo = ' + QuotedStr('*' + edtFiltro.Text + '*');
  9.     mtColunasDestino.Filtered := true;
  10.     mtColunasDestino.Refresh; //add this line
  11.   end;
  12. end;  

If the ghost is still not disappeared, loop through the dataset and see if contains any data, like this:
Code: Pascal  [Select][+][-]
  1. procedure TViewNovaEntidadeRelacionalNovaFK.Button1Click(Sender: TObject);
  2. begin
  3.   mtColunasDestino.First;
  4.   while not mtColunasDestino.EOF do
  5.   begin
  6.     ShowMessage(mtColunasDestino.FieldByName('FIELD_NAME').AsString; //change FIELD_NAME
  7.     mtColunasDestino.Next;
  8.   end;
  9. end;  
How many record do you see now?

LeoBruno

  • Jr. Member
  • **
  • Posts: 64
Re: Weird Problems with TBufDataSet
« Reply #5 on: September 03, 2022, 11:18:34 pm »
Thank you for the reply, but that didn't work;

Code: Pascal  [Select][+][-]
  1. DBGrid.BeginUpdate;
  2. // dataset code
  3. DBGrid.EndUpdate;
or something similar using one of the events.
Lazarus 4.4 FPC 3.2.2 Windows - MetaDarkStyle

LeoBruno

  • Jr. Member
  • **
  • Posts: 64
Re: Weird Problems with TBufDataSet
« Reply #6 on: September 03, 2022, 11:22:28 pm »
Thank you for the reply...

I Used your tips.

The Dataset is Indeed empty.

In fact, removing the test "If not DataSet.IsEmpty" before calling delete, shows me that, because it throws exception stating that the DataSet Is Empty.

I solved the "Delete" problem switching to TMemDataSet.

But now, the filter does nothing at all.

Am I missing something?

Most likely a syncing problem inside TBufDataset, not related to the grid. Please try to refresh the BufDataset after filtering.
Code: Pascal  [Select][+][-]
  1. procedure TViewNovaEntidadeRelacionalNovaFK.FiltraMtDestino;
  2. begin
  3.   mtColunasDestino.Filtered := false;
  4.   mtColunasDestino.Filter := '';
  5.  
  6.   if Trim(edtFiltro.Text) <> '' then
  7.   begin
  8.     mtColunasDestino.Filter := 'NomeCampo = ' + QuotedStr('*' + edtFiltro.Text + '*');
  9.     mtColunasDestino.Filtered := true;
  10.     mtColunasDestino.Refresh; //add this line
  11.   end;
  12. end;  

If the ghost is still not disappeared, loop through the dataset and see if contains any data, like this:
Code: Pascal  [Select][+][-]
  1. procedure TViewNovaEntidadeRelacionalNovaFK.Button1Click(Sender: TObject);
  2. begin
  3.   mtColunasDestino.First;
  4.   while not mtColunasDestino.EOF do
  5.   begin
  6.     ShowMessage(mtColunasDestino.FieldByName('FIELD_NAME').AsString; //change FIELD_NAME
  7.     mtColunasDestino.Next;
  8.   end;
  9. end;  
How many record do you see now?
Lazarus 4.4 FPC 3.2.2 Windows - MetaDarkStyle

LeoBruno

  • Jr. Member
  • **
  • Posts: 64
Re: Weird Problems with TBufDataSet
« Reply #7 on: September 03, 2022, 11:35:21 pm »
Before someone wastes time answering, I reverted the changes, because there are several issues with TMemDataSet and Locate, Filter methods.

https://wiki.lazarus.freepascal.org/How_to_write_in-memory_database_applications_in_Lazarus/FPC#Known_problems

So, I´m back to the beggining.

Thank you for the reply...

I Used your tips.

The Dataset is Indeed empty.

In fact, removing the test "If not DataSet.IsEmpty" before calling delete, shows me that, because it throws exception stating that the DataSet Is Empty.

I solved the "Delete" problem switching to TMemDataSet.

But now, the filter does nothing at all.

Am I missing something?

Most likely a syncing problem inside TBufDataset, not related to the grid. Please try to refresh the BufDataset after filtering.
Code: Pascal  [Select][+][-]
  1. procedure TViewNovaEntidadeRelacionalNovaFK.FiltraMtDestino;
  2. begin
  3.   mtColunasDestino.Filtered := false;
  4.   mtColunasDestino.Filter := '';
  5.  
  6.   if Trim(edtFiltro.Text) <> '' then
  7.   begin
  8.     mtColunasDestino.Filter := 'NomeCampo = ' + QuotedStr('*' + edtFiltro.Text + '*');
  9.     mtColunasDestino.Filtered := true;
  10.     mtColunasDestino.Refresh; //add this line
  11.   end;
  12. end;  

If the ghost is still not disappeared, loop through the dataset and see if contains any data, like this:
Code: Pascal  [Select][+][-]
  1. procedure TViewNovaEntidadeRelacionalNovaFK.Button1Click(Sender: TObject);
  2. begin
  3.   mtColunasDestino.First;
  4.   while not mtColunasDestino.EOF do
  5.   begin
  6.     ShowMessage(mtColunasDestino.FieldByName('FIELD_NAME').AsString; //change FIELD_NAME
  7.     mtColunasDestino.Next;
  8.   end;
  9. end;  
How many record do you see now?
Lazarus 4.4 FPC 3.2.2 Windows - MetaDarkStyle

LeoBruno

  • Jr. Member
  • **
  • Posts: 64
Re: Weird Problems with TBufDataSet
« Reply #8 on: September 03, 2022, 11:57:37 pm »
I found the problem.

It was created by myself (my bad).

Thank you all for helping.

I have a custom DrawColum Method, used for changing the color of the font, in DAR Themed OS, when the Grid is on Selected State.

I went from this:

Code: Pascal  [Select][+][-]
  1. procedure TViewAssets.DBGridDrawColumnCell(Sender: TObject; const Rect: TRect;
  2.   DataCol: Integer; Column: TColumn; State: TGridDrawState);
  3. begin
  4.   if gdSelected in State then
  5.   begin
  6.     if TModelOSUtils.IsDarkTheme then
  7.       TDBGrid(Sender).Canvas.Font.Color := clBlack;
  8.  
  9.     TDBGrid(Sender).DefaultDrawColumnCell(Rect, DataCol, Column, State);
  10.   end;
  11. end;  
  12.  

To this:
Code: Pascal  [Select][+][-]
  1. procedure TViewAssets.DBGridDrawColumnCell(Sender: TObject; const Rect: TRect;
  2.   DataCol: Integer; Column: TColumn; State: TGridDrawState);
  3. begin
  4.   if ((gdSelected in State) and (not TDBGrid(Sender).DataSource.DataSet.IsEmpty)) then
  5.   begin
  6.     if TModelOSUtils.IsDarkTheme then
  7.       TDBGrid(Sender).Canvas.Font.Color := clBlack;
  8.  
  9.     TDBGrid(Sender).DefaultDrawColumnCell(Rect, DataCol, Column, State);
  10.   end;
  11. end;  
  12.  

Lazarus 4.4 FPC 3.2.2 Windows - MetaDarkStyle

LeoBruno

  • Jr. Member
  • **
  • Posts: 64
Re: Weird Problems with TBufDataSet
« Reply #9 on: September 03, 2022, 11:58:33 pm »
I found the problem.

It was caused by myself (my bad).

Thank you all for helping.

I have a custom DrawColum Method, used for changing the color of the font, for DARK Themed OS, when the Grid is on Selected State.

I went from this:

Code: Pascal  [Select][+][-]
  1. procedure TViewAssets.DBGridDrawColumnCell(Sender: TObject; const Rect: TRect;
  2.   DataCol: Integer; Column: TColumn; State: TGridDrawState);
  3. begin
  4.   if gdSelected in State then
  5.   begin
  6.     if TModelOSUtils.IsDarkTheme then
  7.       TDBGrid(Sender).Canvas.Font.Color := clBlack;
  8.  
  9.     TDBGrid(Sender).DefaultDrawColumnCell(Rect, DataCol, Column, State);
  10.   end;
  11. end;  
  12.  

To this:
Code: Pascal  [Select][+][-]
  1. procedure TViewAssets.DBGridDrawColumnCell(Sender: TObject; const Rect: TRect;
  2.   DataCol: Integer; Column: TColumn; State: TGridDrawState);
  3. begin
  4.   if ((gdSelected in State) and (not TDBGrid(Sender).DataSource.DataSet.IsEmpty)) then
  5.   begin
  6.     if TModelOSUtils.IsDarkTheme then
  7.       TDBGrid(Sender).Canvas.Font.Color := clBlack;
  8.  
  9.     TDBGrid(Sender).DefaultDrawColumnCell(Rect, DataCol, Column, State);
  10.   end;
  11. end;  
  12.  
« Last Edit: September 04, 2022, 01:08:56 pm by LeoBruno »
Lazarus 4.4 FPC 3.2.2 Windows - MetaDarkStyle

 

TinyPortal © 2005-2018