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
Apply filter to all columns on a Data Table?
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.
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.
-
- Posts: 55
- Last visit: Thu Jun 24, 2021 3:05 am
-
- Posts: 55
- Last visit: Thu Jun 24, 2021 3:05 am
Re: Apply filter to all columns on a Data Table?
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
$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
-
- Posts: 55
- Last visit: Thu Jun 24, 2021 3:05 am
Re: Apply filter to all columns on a Data Table?
$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
}
$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
}
-
- Posts: 55
- Last visit: Thu Jun 24, 2021 3:05 am