Recent

Author Topic: Filtering a TStringGrid. Suggestions?  (Read 805 times)

EganSolo

  • Sr. Member
  • ****
  • Posts: 334
Filtering a TStringGrid. Suggestions?
« on: June 11, 2025, 11:57:44 pm »
I'm looking for a component that filters TStringGrid content. Before coding, I thought I'd ask if such a component exists.

Thank you for your suggestions!

n7800

  • Sr. Member
  • ****
  • Posts: 399
Re: Filtering a TStringGrid. Suggestions?
« Reply #1 on: June 12, 2025, 12:48:59 am »

EganSolo

  • Sr. Member
  • ****
  • Posts: 334
Re: Filtering a TStringGrid. Suggestions?
« Reply #2 on: June 12, 2025, 01:04:10 am »
n7800: thanks for the links. I guess, I wasn't clear enough: I'm looking for a visual component that sits on top of the grid to let the user type the strings (one per column) that they want to filter on.

The links you provided discuss how to hide the filtered out rows.

n7800

  • Sr. Member
  • ****
  • Posts: 399
Re: Filtering a TStringGrid. Suggestions?
« Reply #3 on: June 12, 2025, 01:31:22 am »
The first message also contains links where you can immediately find a mention of "VirtualTreeView" (which I have not checked, just a quote):

https://forum.lazarus.freepascal.org/index.php/topic,25791.msg157130.html#msg157130

But it seems I understood that you mean a separate component like TListFilterEdit/TListViewFilterEdit? Personally, I do not know such a thing. Perhaps you will write it ))

In one of my programs, I also had to implement a descendant of the TStringGrid class to implement a filter. By the way, I placed them as an additional line under the header (under the column names), in my opinion, more compact than a separate panel.

EganSolo

  • Sr. Member
  • ****
  • Posts: 334
Re: Filtering a TStringGrid. Suggestions?
« Reply #4 on: June 12, 2025, 03:56:35 am »
My initial attempt was similar to your solution: I instantiated a separate StringGrid to use as a filter. The somewhat (well, slightly) tricky part is that I want interactive filtering, which hides or shows rows in the grid as the user modifies the filter. My data requirement is modest (less than a thousand rows), and thus it is possible. However, it implies that I need to keep track of the internal editor the grid uses and hook into that editor's OnKeyPress and OnKeydown events. It's feasible, but the code is more obscure.

I'm going to think some more ...

n7800

  • Sr. Member
  • ****
  • Posts: 399
Re: Filtering a TStringGrid. Suggestions?
« Reply #5 on: June 12, 2025, 04:30:39 am »
I also used a different solution. I created a TEdit for each column at runtime and aligned them to cells via the OnDrawCell event. Just a small demo (I can't share the program itself):
Code: Pascal  [Select][+][-]
  1. procedure TForm1.StringGrid1DrawCell(Sender: TObject; aCol, aRow: Integer; aRect: TRect; aState: TGridDrawState);
  2. var
  3.   lGrid: TMyStringGrid absolute Sender;
  4. begin
  5.   if aRow = 1 then
  6.     case aCol of
  7.       cColNumbers: lGrid.edtNumbers.BoundsRect := aRect;
  8.       cColName   : lGrid.edtName   .BoundsRect := aRect;
  9.       cColUnits  : lGrid.edtUnits  .BoundsRect := aRect;
  10.     end;
  11. end;
  12.  

So all filters are interactive. Of course, instead of constants, I could have used a TEdit array and looped through it, but that wasn't necessary in this program. The only thing left was to do all this in a separate component, not a form, but I did not finish it.

paweld

  • Hero Member
  • *****
  • Posts: 1434
Re: Filtering a TStringGrid. Suggestions?
« Reply #6 on: June 12, 2025, 01:58:09 pm »
maybe instead of TStringGrid use TDBGrid and dxDBGridController ( https://wiki.freepascal.org/TdxDBGridController#Global_search ) and keep the data in BufDataSet instead of in the grid
Best regards / Pozdrawiam
paweld

Hansvb

  • Hero Member
  • *****
  • Posts: 824
Re: Filtering a TStringGrid. Suggestions?
« Reply #7 on: June 12, 2025, 06:33:20 pm »
Coincidentally, this week I have also been looking for how to filter a TStringGrid. My conclusion was, replace TStringGrid with a TDBGrid. In my case, I retrieved the data with a TSQlquery anyway. TSQlquery has a ServerFilter property. That works well.

Quote
https://www.freepascal.org/docs-html/fcl/sqldb/tsqlquery.serverfilter.html

Quote
https://www.youtube.com/watch?v=VlU925xHOAc

Nicole

  • Hero Member
  • *****
  • Posts: 1210
Re: Filtering a TStringGrid. Suggestions?
« Reply #8 on: June 12, 2025, 10:52:34 pm »
What about TDBStringGrid?
Or even TDynamicGrid?

The first one has a filter event and the second one a sort function, which works like the click at the columns in Excel.

 

TinyPortal © 2005-2018