Recent

Author Topic: Clear stringgrid rows with only "white space" (SOLVED)  (Read 5604 times)

mijen67

  • Full Member
  • ***
  • Posts: 130
  • It's hard to beat the bandwidth of a flying DVD
Clear stringgrid rows with only "white space" (SOLVED)
« on: January 30, 2018, 10:00:19 pm »
Is there an easy way to delete stringgrid rows with only white space (tab, space).

The stringgrid is formatted with fixed columns with headers and a variable number of rows. Users can enter data including white space, and thereby empty rows. I need to remove these before processing data.

Kind regards,
Michael
« Last Edit: January 31, 2018, 11:24:02 pm by mijen67 »

Bart

  • Hero Member
  • *****
  • Posts: 5274
    • Bart en Mariska's Webstek
Re: Clear stringgrid rows with only "white space"
« Reply #1 on: January 30, 2018, 10:10:22 pm »
Modify the IsEmptyRow function to your needs, then loop through the rows (starting at last row) and delete as needed. Use BeginUpdate/EndUpdate.

Bart

mijen67

  • Full Member
  • ***
  • Posts: 130
  • It's hard to beat the bandwidth of a flying DVD
Re: Clear stringgrid rows with only "white space"
« Reply #2 on: January 30, 2018, 10:54:47 pm »
Modify the IsEmptyRow function to your needs, then loop through the rows (starting at last row) and delete as needed. Use BeginUpdate/EndUpdate.

Bart

Is the IsEmptyRow function inherited from a parent class of stringgrid? It doesn't seem to be part of the stringgrid itself?
http://lazarus-ccr.sourceforge.net/docs/lcl/grids/tstringgrid.html and also it isn't mentioned in doc: http://wiki.lazarus.freepascal.org/TStringGrid

Or is IsEmptyRow just a customly defined function?

Could you please elaborate on or point to where BeginUpdate and EndUpdate are documented? I found some hints, but not core documentation, e.g. https://stackoverflow.com/questions/3712229/delphi-tstringgrid-flicker

Bart

  • Hero Member
  • *****
  • Posts: 5274
    • Bart en Mariska's Webstek
Re: Clear stringgrid rows with only "white space"
« Reply #3 on: January 30, 2018, 11:13:17 pm »
Is the IsEmptyRow function inherited from a parent class of stringgrid? It doesn't seem to be part of the stringgrid itself?

It's a protected function of TCustomGrid.
Unfortunately it is not virtual so you cannot override it.
Just duplicate the code as needed.

Code: Pascal  [Select][+][-]
  1. function TCustomGrid.IsEmptyRow(ARow: Integer): Boolean;
  2. var
  3.   i: Integer;
  4. begin
  5.   Result := False;
  6.   for i:=FixedCols to ColCount-1 do
  7.   if GetCells(i, ARow)<>'' then begin
  8.     Exit;
  9.   end;
  10.   Result := True;
  11. end;
  12.  

Instead of
Code: Pascal  [Select][+][-]
  1.   if GetCells(i, ARow)<>'' then
  2.  

use
Code: Pascal  [Select][+][-]
  1.   if Trim(GetCells(i, ARow))<>'' then  //this will retrun true if GetCells retruns only whitespace
  2.  


Could you please elaborate on or point to where BeginUpdate and EndUpdate are documented?

Imagine you have a large grid and you want to update many cells.

Code: Pascal  [Select][+][-]
  1. var
  2.   Grid: TStringGrid;
  3. ...
  4. begin
  5.   Grid.BeginUpdate;
  6.   try
  7.     for R := 0 to Grid.RowCount-1 do
  8.     begin
  9.       for C := 0 to Grid.ColCount-1 do
  10.       begin
  11.         Grid.Cells[C,R] := '*'+Cells[C,R]+'*';
  12.       end;
  13.     end;
  14.   finally
  15.     Grid.EndUpdate
  16.   end;//try..finally
  17. end;
  18.  

If you leave out the BeginUpdate/EndUpdate, after each Grid.Cells[C,R] := '*'+Cells[C,R]+'*', the Grid will redraw it's contents to the screen.
This will cause a lot of flicker and it will take more time.
If you use BeginUpdate/EndUpdate, the Grid will not draw anything until EndUpdate is called.

This is also why you need to use the try..finally construct, otherwise if an exception occurs EndUpdate is never called, and the Grid does not draw anymore to the screen.

Bart

PatBayford

  • Full Member
  • ***
  • Posts: 125
Re: Clear stringgrid rows with only "white space"
« Reply #4 on: January 31, 2018, 01:12:48 am »
I'm not sure that BeginUpdate/EndUpdate are fully documented anywhere - my understanding is that BeginUpdate simply blocks the redraw message, whereas EndUpdate restores the status quo - the sole purpose is to prevent unsightly, and possibly distracting, screen redraws while significant processing is carried out on the contents. Similar methods exist for list boxes etc.
Lazarus 1.8.0 FPC 3.0.2 SVN 56594 Windows 10 64bit (i386-win32-win32/win64)

Thaddy

  • Hero Member
  • *****
  • Posts: 14197
  • Probably until I exterminate Putin.
Re: Clear stringgrid rows with only "white space"
« Reply #5 on: January 31, 2018, 09:08:58 am »
I'm not sure that BeginUpdate/EndUpdate are fully documented anywhere - my understanding is that BeginUpdate simply blocks the redraw message, whereas EndUpdate restores the status quo - the sole purpose is to prevent unsightly, and possibly distracting, screen redraws while significant processing is carried out on the contents. Similar methods exist for list boxes etc.

The sole purpose is to speed things up by skipping redraws altogether... Since redraws are computational expensive.
Specialize a type, not a var.

mijen67

  • Full Member
  • ***
  • Posts: 130
  • It's hard to beat the bandwidth of a flying DVD
Re: Clear stringgrid rows with only "white space"
« Reply #6 on: January 31, 2018, 11:23:16 pm »
It's a protected function of TCustomGrid.
Unfortunately it is not virtual so you cannot override it.
Just duplicate the code as needed.

Perfect, thanks!

 

TinyPortal © 2005-2018