Apply filter to all columns on a Data Table?

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 2 years and 11 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
ClipperXTP
Posts: 55
Last visit: Thu Jun 24, 2021 3:05 am

Apply filter to all columns on a Data Table?

Post by ClipperXTP »

Hi All
I have a datagridview which is populated by various functions when a user clicks a choice of buttons in my project. The results are converted to datatables before updating the datagridview.

The columns of the datagridview will therefore be different depending on which query a user is running.

I would like to be able to filter on all columns.

I currently have a filter on a more simple datagridview which always returns a column Name and which I can filter on column name as follows:

$datagridviewmachines.DataSource.DefaultView.RowFilter = "Name LIKE '*$($textboxfilter.Text)*'"

In my latest project, the datagridview may have 3 columns or 10 columns and the columns will not have the same headings.
Is it possible though to filter on ALL columns of whatever have been returned to the datagridview?

thanks in advance
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Apply filter to all columns on a Data Table?

Post by jvierra »

It's possible. Just write the filter code.
ClipperXTP
Posts: 55
Last visit: Thu Jun 24, 2021 3:05 am

Re: Apply filter to all columns on a Data Table?

Post by ClipperXTP »

I am nearly there I think although my code doesn't seem to be grabbing the column name. Any ideas?

$filter = $($textboxfilter.Text)

FOREACH ($Column in $datagridviewmachines.Columns)
{
Write-Host $($column.columnname)
$RowFilter += "{0} Like '%{1}%' OR " -f $($Column.ColumnName), $Filter
}

$RowFilter = $RowFilter -replace ".{4}$"

Write-Host $RowFilter

$datagridviewmachines.DataSource.DefaultView.RowFilter = $RowFilter
ClipperXTP
Posts: 55
Last visit: Thu Jun 24, 2021 3:05 am

Re: Apply filter to all columns on a Data Table?

Post by ClipperXTP »

$textboxfilter_textchanged={

$filter = $($textboxfilter.Text)

FOREACH ($Column in $datagridviewmachines.Columns)
{

$RowFilter += "{0} Like '%{1}%' OR " -f $($Column.headertext), $Filter
}

# chop off trailing ' OR '
$RowFilter = $RowFilter -replace ".{4}$"



$datagridviewmachines.DataSource.DefaultView.RowFilter = $RowFilter

}
ClipperXTP
Posts: 55
Last visit: Thu Jun 24, 2021 3:05 am

Re: Apply filter to all columns on a Data Table?

Post by ClipperXTP »

Got it working as above.
This topic is 2 years and 11 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