Search as you type

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 7 years 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
User avatar
alvalenz79
Posts: 36
Last visit: Fri Jan 26, 2018 11:23 am

Search as you type

Post by alvalenz79 »

Is there a way to filter as you type? I want the function to search my datagrid without using a button.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Search as you type

Post by jvierra »

You would have to write custom search code. This can be done with a datatable and by setting the filter in the dataview. Performance will not be good for other than very small datasets.

Consider using a listbox with autocomplete.
User avatar
alvalenz79
Posts: 36
Last visit: Fri Jan 26, 2018 11:23 am

Re: Search as you type

Post by alvalenz79 »

would 300 accounts be considered small?
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Search as you type

Post by jvierra »

Probably would work. Use a combobox loaded with the names then use the selected name to set a filter.
User avatar
alvalenz79
Posts: 36
Last visit: Fri Jan 26, 2018 11:23 am

Re: Search as you type

Post by alvalenz79 »

okay, Thank for the advice.
User avatar
dan.potter
Posts: 709
Last visit: Wed Nov 14, 2018 11:39 am

Re: Search as you type

Post by dan.potter »

If you're loading your datagridview with a datatable you can filter the results. I assume you can do it on the textchanged event but it may be slow.

Where $dt is my datatable.

textchanged event

$filter = $textbox1.text
$DT.defaultview.rowfilter = "hostname like '%$filter%'"
User avatar
dan.potter
Posts: 709
Last visit: Wed Nov 14, 2018 11:39 am

Re: Search as you type

Post by dan.potter »

for example.

  1. $form1_Load={
  2.    
  3.     $Data = Get-process
  4.     $script:dt = ConvertTo-DataTable -InputObject $data
  5.     $datagridview1.DataSource = $dt
  6. }
  7.  
  8.  
  9.  
  10. $textbox1_TextChanged={
  11.    
  12.     $filter = $textbox1.text
  13.     $DT.defaultview.rowfilter = "name like '%$filter%'"
  14.    
  15. }
User avatar
alvalenz79
Posts: 36
Last visit: Fri Jan 26, 2018 11:23 am

Re: Search as you type

Post by alvalenz79 »

@ dan

I have modified my script to mimic what you recommend. Lines 278 -293

$form1_Load={

$Data = Get-process
$script:dt = ConvertTo-DataTable -InputObject $data
$datagridview1.DataSource = $dt
}


line 367-371

$textbox1_TextChanged={

$filter = $textbox1.text
$DT.defaultview.rowfilter = "name like '%$filter%'"
I get he following error when I type anything in the box.

The property 'rowfilter' cannot be found on this object. Verify that the property exists and can be set.
Attachments
ADupdaterHelp.psf
(29.42 KiB) Downloaded 156 times
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Search as you type

Post by jvierra »

Here is the technique:
  1. [System.Data.DataTable]$dt = Get-process | ConvertTo-DataTable
  2. $datagridview1.DataSource = $dt
  3. $dt.DefaultView.RowFilter = "Name like '%joe%'"
User avatar
dan.potter
Posts: 709
Last visit: Wed Nov 14, 2018 11:39 am

Re: Search as you type

Post by dan.potter »

:D Good one, I was wondering why that convert-todatatable wasn't working.
This topic is 7 years 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