Public/Set-WFDataGridViewFilter.ps1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
function Set-WFDataGridViewFilter { <# .SYNOPSIS The function Set-WFDataGridViewFilter helps to only show specific entries with a specific value .DESCRIPTION The function Set-WFDataGridViewFilter helps to only show specific entries with a specific value. The data needs to be in a DataTable Object. You can use ConvertTo-DataTable to convert your PowerShell object into a DataTable object. .PARAMETER DataGridView Specifies the DataGridView control where the data will be filtered .PARAMETER DataTable Specifies the DataTable object that is most likely the original source of the DataGridView .PARAMETER Filter Specifies the string to search .PARAMETER ColumnName Specifies to search in a specific column name .PARAMETER AllColumns Specifies to search all the column .EXAMPLE PS C:\> Set-WFDataGridViewFilter -DataGridView $datagridview1 -DataTable $ProcessesDT -AllColumns -Filter $textbox1.Text .EXAMPLE PS C:\> Set-WFDataGridViewFilter -DataGridView $datagridview1 -DataTable $ProcessesDT -ColumnName "Name" -Filter $textbox1.Text .NOTES Author: Francois-Xavier Cat Twitter:@LazyWinAdm www.lazywinadmin.com github.com/lazywinadmin RowFilter Help: https://msdn.microsoft.com/en-us/library/system.data.datacolumn.expression(VS.80).aspx #> [CmdletBinding(DefaultParameterSetName = 'AllColumns', SupportsShouldProcess = $true)] param ( [Parameter(Mandatory = $true)] [System.Windows.Forms.DataGridView]$DataGridView, [Parameter(Mandatory = $true)] [System.Data.DataTable]$DataTable, [Parameter(Mandatory = $true)] [String]$Filter, [Parameter(ParameterSetName = 'OneColumn', Mandatory = $true)] [String]$ColumnName, [Parameter(ParameterSetName = 'AllColumns', Mandatory = $true)] [Switch]$AllColumns ) BEGIN { Add-Type -AssemblyName System.Windows.Forms } PROCESS { IF ($PSBoundParameters['AllColumns']) { FOREACH ($Column in $DataTable.Columns) { $RowFilter += "{0} Like '%{1}%' OR " -f $($Column.ColumnName), $Filter } # Remove the last 'OR' $RowFilter = $RowFilter -replace " OR $", '' } IF ($PSBoundParameters['ColumnName']) { $RowFilter = "$ColumnName LIKE '%$Filter%'" } IF ($PSCmdlet.ShouldProcess($DataGridView, "Filter the content on $filter")) { $DataTable.defaultview.rowfilter = $RowFilter Import-WFDataGridViewItem -DataGridView $DataGridView -Item $DataTable } } END { Remove-Variable -Name $RowFilter -ErrorAction 'SilentlyContinue' | Out-Null } } |