Recent

Author Topic: [SOLVED] Prevent DBGrid to show empty row in case 0 records returned  (Read 1262 times)

Zvoni

  • Hero Member
  • *****
  • Posts: 3140
OK, i'll bite.

I have a Form with 4 DBGrids, each connected to its own SQLquery-Datasource.
User clicks/selects a record on the first Grid, which executes a new Query on the Datasource of the Second grid and so on....

In case the second Datasource returns 0 records, it still shows me an "empty" line/row below the (fixed) row with Column-Titles.

How can i avoid that empty line?
I'd like the Grid to show just the (fixed) row with Column-Titles (each grid has defined Columns with titles, width etc.).

I thought Clear/ClearRows was the answer, but that throws out the Row with the Columns-Titles, too.

Any ideas?
Logic implies it might not even be a "property" of the Grid, but something of the Dataset or Datasource, but i haven't found anything pointing in that direction
« Last Edit: October 27, 2025, 08:48:22 am by Zvoni »
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

LacaK

  • Hero Member
  • *****
  • Posts: 702
Re: Prevent DBGrid to show empty row in case 0 records returned
« Reply #1 on: October 23, 2025, 02:27:06 pm »
As far as I can say it is default behavior - if DataSet is empty there is still 1 empty row in TDBGrid (I guess that this one empty row allow you to start write there to be able to append new record).
Can you try exclude from TDBGrid.Options: dgIndicator, dgEditing, dgAlwaysShowEditor whether something changed?
« Last Edit: October 23, 2025, 02:31:08 pm by LacaK »

Zvoni

  • Hero Member
  • *****
  • Posts: 3140
Re: Prevent DBGrid to show empty row in case 0 records returned
« Reply #2 on: October 23, 2025, 02:44:29 pm »
As far as I can say it is default behavior - if DataSet is empty there is still 1 empty row in TDBGrid (I guess that this one empty row allow you to start write there to be able to append new record).
Can you try exclude from TDBGrid.Options: dgIndicator, dgEditing, dgAlwaysShowEditor whether something changed?
Thx for the answer.
Hmmm.... will have to try that.....
Otherwise it is as it is.
It's just "cosmetic"
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

Soner

  • Sr. Member
  • ****
  • Posts: 325
Re: Prevent DBGrid to show empty row in case 0 records returned
« Reply #3 on: October 24, 2025, 10:27:04 pm »
The only solution is to disable the query.
If you are using ZEOS components:
  if ZQuery1.RecordCount=0 then ZQuery1.Active:=false;
If you are using SQLDB:
  if SQLQuery1.RowsAffected=0 then SQLQuery1.Active:=false;

rvk

  • Hero Member
  • *****
  • Posts: 6886
Re: Prevent DBGrid to show empty row in case 0 records returned
« Reply #4 on: October 24, 2025, 10:30:34 pm »
The only solution is to disable the query.
That will also remove any fields automatically defined in the dataset and grid.
So the (fixed) row with Column-Titles don't show anymore.

You should have to save it and create the grid columns statically.

Soner

  • Sr. Member
  • ****
  • Posts: 325
Re: Prevent DBGrid to show empty row in case 0 records returned
« Reply #5 on: October 24, 2025, 10:37:22 pm »
The only solution is to disable the query.
That will also remove any fields automatically defined in the dataset and grid.
So the (fixed) row with Column-Titles don't show anymore.

You should have to save it and create the grid columns statically.
It is useless because there is still a blank line, possibly a Delphi compatibility issue. We should fix TDBGrid.

rvk

  • Hero Member
  • *****
  • Posts: 6886
Re: Prevent DBGrid to show empty row in case 0 records returned
« Reply #6 on: October 24, 2025, 10:43:20 pm »
Without an active dataset there are no fields and the tdbgrid can't fill the column titles so it's useless anyway to fix it like that.

It's better to fix it with a valid empty dataset and remove the empty line. But only when there is no edit option because the empty line serves a purpose at this moment (being the current dataset pointer, even if the dataset is empty) and you need it for adding records in editing mode.

Soner

  • Sr. Member
  • ****
  • Posts: 325
Re: Prevent DBGrid to show empty row in case 0 records returned
« Reply #7 on: October 24, 2025, 11:19:47 pm »
I have the solution. You need to change the file lazarus/lcl/dbgrids.pas as shown in the image; look at the two lines marked in red.

EDIT:
I noticed that nothing is displayed if the result of the quer is only one data row. But the solution is in this function.
« Last Edit: October 24, 2025, 11:47:19 pm by Soner »

Zvoni

  • Hero Member
  • *****
  • Posts: 3140
Re: Prevent DBGrid to show empty row in case 0 records returned
« Reply #8 on: October 25, 2025, 02:00:16 pm »
Without an active dataset there are no fields and the tdbgrid can't fill the column titles so it's useless anyway to fix it like that.

It's better to fix it with a valid empty dataset and remove the empty line. But only when there is no edit option because the empty line serves a purpose at this moment (being the current dataset pointer, even if the dataset is empty) and you need it for adding records in editing mode.
Rvk, not correct.
I have TColums defined that connect to the fields of the dataset. And those have the titles.
I‘m not using the field names
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

rvk

  • Hero Member
  • *****
  • Posts: 6886
Re: Prevent DBGrid to show empty row in case 0 records returned
« Reply #9 on: October 25, 2025, 02:27:50 pm »
Rvk, not correct.
I have TColums defined that connect to the fields of the dataset. And those have the titles.
I‘m not using the field names
Yes, those are statically assigned during design time.
I was talking about the dynamically assigned columns from the dataset.

The only solution is to disable the query.
That will also remove any fields automatically defined in the dataset and grid.
So the (fixed) row with Column-Titles don't show anymore.

You should have to save it and create the grid columns statically.

I normally have my columns defined dynamically because of I change the SQL, the column change with it.
« Last Edit: October 25, 2025, 02:29:41 pm by rvk »

Zvoni

  • Hero Member
  • *****
  • Posts: 3140
Re: [SOLVED] Prevent DBGrid to show empty row in case 0 records returned
« Reply #10 on: October 27, 2025, 08:50:00 am »
OK, found it.
Soner brought me on the right track with his Active:=False;

In a Nutshell it boils down to
If RS.RecordCount=0 Then RS.Close;
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

rvk

  • Hero Member
  • *****
  • Posts: 6886
Re: [SOLVED] Prevent DBGrid to show empty row in case 0 records returned
« Reply #11 on: October 27, 2025, 08:56:38 am »
In a Nutshell it boils down to
If RS.RecordCount=0 Then RS.Close;
Sure. If the columns of the grid are statically created during design time, this is a valid solution.
Otherwise you loose the column definitions.

But if it's works in your situation, it's a good solution for you.

Zvoni

  • Hero Member
  • *****
  • Posts: 3140
Re: [SOLVED] Prevent DBGrid to show empty row in case 0 records returned
« Reply #12 on: October 27, 2025, 11:22:34 am »
In a Nutshell it boils down to
If RS.RecordCount=0 Then RS.Close;
Sure. If the columns of the grid are statically created during design time, this is a valid solution.
Otherwise you loose the column definitions.

But if it's works in your situation, it's a good solution for you.
Yeah. Though i don't like to work with DB-bound Controls, i have to in this case.
And since i'm used to define my Columns for e.g. StringGrid, i've adopted the same approach for DBGrid
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

 

TinyPortal © 2005-2018