Recent

Author Topic: Critique this code that implements filtering on a TStringGrid.  (Read 728 times)

vfclists

  • Hero Member
  • *****
  • Posts: 1146
    • HowTos Considered Harmful?
The links consulted were:


https://forum.lazarus.freepascal.org/index.php?topic=25791.0

https://forum.lazarus.freepascal.org/index.php?topic=53051.0

https://forum.lazarus.freepascal.org/index.php/topic,35488.0

The idea is to set the RowHeights[aRow] := 0 if the cell value does not match the value in the TEdit.

A lot of the examples focus on the DrawCell event of CellHeight event to set the row height, but I just wanted to avoid that complexity.

The column being checked is just the first column.

Code: Pascal  [Select][+][-]
  1. procedure TForm1.filterEditChange(Sender: TObject);
  2. var
  3.  fltText: string;
  4.  
  5.  procedure ProcessEmpty;
  6.  var
  7.    Row: integer;
  8.  begin
  9.    BeginFormUpdate;
  10.    for Row := 0 to stringGrid.RowCount - 1 do
  11.    begin
  12.      stringGrid.RowHeights[Row] := stringGrid.DefaultRowHeight;
  13.    end;
  14.    EndFormUpdate;
  15.  end;
  16.  
  17.  procedure ProcessAll;
  18.  var
  19.    Row: integer;
  20.  begin
  21.    BeginFormUpdate;
  22.    for Row := 1 to stringGrid.RowCount - 1 do
  23.      if (not (ContainsText(stringGrid.Cells[1,Row], fltText))) then
  24.        stringGrid.RowHeights[Row] := 0
  25.      else
  26.        stringGrid.RowHeights[Row] := stringGrid.DefaultRowHeight;
  27.  
  28.    EndFormUpdate;
  29.  end;
  30.  
  31. begin
  32.   fltText := TEdit(Sender).Text;
  33.   if fltText = '' then
  34.     ProcessEmpty
  35.   else
  36.     ProcessAll;
  37. end;
  38.  

I wonder if this approach is wise if the string grid holds millions of items.

Are the cell drawing events better discussed in the above links inherently better? I assume the events don't fire unless the cell comes into view. Amirite?
« Last Edit: August 31, 2024, 12:09:40 am by vfclists »
Lazarus 3.0/FPC 3.2.2

jamie

  • Hero Member
  • *****
  • Posts: 6695
Re: Critique this code that implements filtering on a TStringGrid.
« Reply #1 on: August 31, 2024, 04:20:23 pm »
   It's insane to think you are going to scroll through millions of entries in a string grid component, you would run out of space before you could get to some usable state.

  Use a Database, that is what there designed for.

 
  But, if you insist not to, the proper way is to scan a master file of entries, building a selected file that your selected stringgrid will display.

  But, even at that point, the generated selected file maybe large, too. You then would set up your string grid to hold only x-number of records and you scroll backwards and forwards from the file in increments

  The Object properties of the string grids for each entry can be used to as a file pointer to the entry point of the file etc.

It's your call.
The only true wisdom is knowing you know nothing

vfclists

  • Hero Member
  • *****
  • Posts: 1146
    • HowTos Considered Harmful?
Re: Critique this code that implements filtering on a TStringGrid.
« Reply #2 on: August 31, 2024, 06:03:02 pm »
   It's insane to think you are going to scroll through millions of entries in a string grid component, you would run out of space before you could get to some usable state.

  Use a Database, that is what there designed for.

 
  But, if you insist not to, the proper way is to scan a master file of entries, building a selected file that your selected stringgrid will display.

  But, even at that point, the generated selected file maybe large, too. You then would set up your string grid to hold only x-number of records and you scroll backwards and forwards from the file in increments

  The Object properties of the string grids for each entry can be used to as a file pointer to the entry point of the file etc.

It's your call.

I started old thread - https://forum.lazarus.freepascal.org/index.php/topic,20199.0.html- after getting tired of having to see every data structure problem as one to be fixed by databases, but I guess in this case of the filterable string grid I will have to turn back to database tables.

In memory sqlite is my preferred approach here, but I supposed temporary sqlite databases will do.
Lazarus 3.0/FPC 3.2.2

sfeinst

  • Full Member
  • ***
  • Posts: 235
Re: Critique this code that implements filtering on a TStringGrid.
« Reply #3 on: August 31, 2024, 06:43:36 pm »
The way I filter strindgrids is by usinf 2 lists.  I'm using the term "lists" generically - not an actual freepascal data structure.  One list contains all data, the other the filtered list.  Then whenever I filter the data (rebuilding the filtered list), I repopulate the stringgrid instead of trying to hide/show rows.  Makes doing things like summarizing totals a lot easier.

I do this with thousands of rows.  But if you're looking at millions, that could be time consuming.

wp

  • Hero Member
  • *****
  • Posts: 12416
Re: Critique this code that implements filtering on a TStringGrid.
« Reply #4 on: August 31, 2024, 06:48:52 pm »
The way I filter strindgrids is by usinf 2 lists.  I'm using the term "lists" generically - not an actual freepascal data structure.  One list contains all data, the other the filtered list.  Then whenever I filter the data (rebuilding the filtered list), I repopulate the stringgrid instead of trying to hide/show rows.  Makes doing things like summarizing totals a lot easier.
Yes that's what I would do, too. And I'd even go a step further: use a TDrawGrid rather than a TStringGrid to avoid double storage of the strings. You only have to provide a string-drawing routine.

dbannon

  • Hero Member
  • *****
  • Posts: 3156
    • tomboy-ng, a rewrite of the classic Tomboy
Re: Critique this code that implements filtering on a TStringGrid.
« Reply #5 on: September 01, 2024, 01:28:45 am »
I use a TListView that may have more that 20,000 items. It runs in virtual mode with the actual data in a FPList. For searching, I maintain two other FPLists, both just indexes (and I cast the fplist pointer to an long int and that is the index into the data list).  Of these two index lists, one is alphabetical, the other date sorted.

If the user is searching interactively, I remove the items from the active list that do not match the seach. So, much faster as the search progresses. Down side is need to regenerate those index lists.

Overall, works well and is relatively simple.
Lazarus 3, Linux (and reluctantly Win10/11, OSX Monterey)
My Project - https://github.com/tomboy-notes/tomboy-ng and my github - https://github.com/davidbannon

jamie

  • Hero Member
  • *****
  • Posts: 6695
Re: Critique this code that implements filtering on a TStringGrid.
« Reply #6 on: September 01, 2024, 03:05:54 pm »
20k items are nothing for a in memory accounting system, but Millions was stated!
The only true wisdom is knowing you know nothing

 

TinyPortal © 2005-2018