Forum > LCL

Critique this code that implements filtering on a TStringGrid.

(1/2) > >>

vfclists:
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  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---procedure TForm1.filterEditChange(Sender: TObject);var fltText: string;  procedure ProcessEmpty; var   Row: integer; begin   BeginFormUpdate;   for Row := 0 to stringGrid.RowCount - 1 do   begin     stringGrid.RowHeights[Row] := stringGrid.DefaultRowHeight;   end;   EndFormUpdate; end;  procedure ProcessAll; var   Row: integer; begin   BeginFormUpdate;   for Row := 1 to stringGrid.RowCount - 1 do     if (not (ContainsText(stringGrid.Cells[1,Row], fltText))) then       stringGrid.RowHeights[Row] := 0     else       stringGrid.RowHeights[Row] := stringGrid.DefaultRowHeight;    EndFormUpdate; end; begin  fltText := TEdit(Sender).Text;  if fltText = '' then    ProcessEmpty  else    ProcessAll;end; 
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?

jamie:
   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.

vfclists:

--- Quote from: jamie 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.

--- End quote ---

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.

sfeinst:
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:

--- Quote from: sfeinst 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.

--- End quote ---
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.

Navigation

[0] Message Index

[#] Next page

Go to full version