Gridview with textbox filter - slow...

Ask questions about creating Graphical User Interfaces (GUI) in PowerShell and using WinForms controls.
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 1 year and 4 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
mwilliamsocc
Posts: 10
Last visit: Wed Nov 16, 2022 8:41 am
Has voted: 1 time

Gridview with textbox filter - slow...

Post by mwilliamsocc »

powershell 2022.

I am using an edit box to filter strings I have populated in a single column gridview. It works - and I love it, but its slow.

Based on the code I'm using which I believe I found elsewhere here - is there a way to make it run faster? I am pulling in a list of GPO's, and there are probably 500 items...
  1. $textbox2_TextChanged = {
  2.         $datagridview1.CurrentCell = $null
  3.         for ($i = 0; $i -lt $datagridview1.RowCount; $i++)
  4.         {
  5.             if ($datagridview1.Rows[$i].Cells[0].value -notlike "*$($textbox2.text)*")
  6.             { $datagridview1.Rows[$i].Visible = $false }
  7.             else
  8.             { $datagridview1.Rows[$i].Visible = $true }
  9.         }
  10.     }
by jvierra » Tue Nov 15, 2022 5:31 pm
You ignored the most basic issue. Use a DataTable and filter that. Your code is not very good, and I do not know of any programmer that would try to code a filter like that. A Listbox can be data bound to a DataTable object and the table object can be directly filters.

I recommend learning a bit about PowerShell beyond just old batch or Basic language methods. Remember that you are using and advanced hi-tech coding system. Old ideas won't work well, and things get worse when you apply that to WinForms.

Here is a PSF with a demo of how to use data bound listboxes. Two files. Put both in the same folder.
Go to full post
User avatar
brittneyr
Site Admin
Posts: 1654
Last visit: Thu Mar 28, 2024 6:54 am
Answers: 39
Been upvoted: 30 times

Re: Gridview with textbox filter - slow...

Post by brittneyr »

[Topic moved by moderator to PowerShell GUIs forum]
Brittney
SAPIEN Technologies, Inc.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Gridview with textbox filter - slow...

Post by jvierra »

Yes. Use a DataBound Listbox and filter the underlying DataTable. It takes only one line of code to do that, and it is almost instantaneous for less than a thousand strings.
mwilliamsocc
Posts: 10
Last visit: Wed Nov 16, 2022 8:41 am
Has voted: 1 time

Re: Gridview with textbox filter - slow...

Post by mwilliamsocc »

wow... thanks. that will come in handy now and in the future. not sure why that didnt cross my mind.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Gridview with textbox filter - slow...

Post by jvierra »

Another simple method if you have a small number of rows is disable updates before the loop and re-enable them after the loop. In a loop each time you make a row hidden the whole DGV will be redrawn. That may be the most time consuming issue.

To disable layout updates: $form1.SuspendLayout()
To re-enable layout updates: $form1.ResumeLayout()
mwilliamsocc
Posts: 10
Last visit: Wed Nov 16, 2022 8:41 am
Has voted: 1 time

Re: Gridview with textbox filter - slow...

Post by mwilliamsocc »

welp.. I did give it a go converting my initial "textchanged" input box event to work on a listbox - the issue I had there was the "visible" property is not valid for a listbox.

So I'm assuming simply reverse engineering what I'm using right now isn't going to work with a listbox.

Any further pointers, or even a spoiler would be delightful.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Gridview with textbox filter - slow...

Post by jvierra »

You ignored the most basic issue. Use a DataTable and filter that. Your code is not very good, and I do not know of any programmer that would try to code a filter like that. A Listbox can be data bound to a DataTable object and the table object can be directly filters.

I recommend learning a bit about PowerShell beyond just old batch or Basic language methods. Remember that you are using and advanced hi-tech coding system. Old ideas won't work well, and things get worse when you apply that to WinForms.

Here is a PSF with a demo of how to use data bound listboxes. Two files. Put both in the same folder.
Attachments
StateCity.csv
(2.56 MiB) Downloaded 125 times
Demo-ListboxDatabindingWithViews.psf
(33.18 KiB) Downloaded 125 times
mwilliamsocc
Posts: 10
Last visit: Wed Nov 16, 2022 8:41 am
Has voted: 1 time

Re: Gridview with textbox filter - slow...

Post by mwilliamsocc »

Oh I do not disagree with anything you said. Its been about a 31 year long learning process but with very little time to dedicate to it because I'm tasked with so many other duties. And yes, I did misunderstand your initial reply - the second one got through my head a bit better.

Your help is appreciated.
This topic is 1 year and 4 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked