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.
Post Reply
ClipperXTP
Posts: 50
Last visit: Fri Apr 16, 2021 8:55 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: 14762
Last visit: Mon Apr 19, 2021 4:16 pm
Answers: 9
Has voted: 3 times
Been upvoted: 7 times

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

Post by jvierra »

It's possible. Just write the filter code.

ClipperXTP
Posts: 50
Last visit: Fri Apr 16, 2021 8:55 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: 50
Last visit: Fri Apr 16, 2021 8:55 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: 50
Last visit: Fri Apr 16, 2021 8:55 am

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

Post by ClipperXTP »

Got it working as above.

Post Reply