diff --git a/WinFormPS.psm1 b/WinFormPS.psm1 index 8baa8cf..f438925 100644 --- a/WinFormPS.psm1 +++ b/WinFormPS.psm1 @@ -446,125 +446,124 @@ function Enable-TabControl } }#Enable-TabControl -function Find-DataGridViewValue -{ -<# - .SYNOPSIS - The Find-DataGridViewValue function helps you to find a specific value and select the cell, row or to set a fore and back color. - - .DESCRIPTION - The Find-DataGridViewValue function helps you to find a specific value and select the cell, row or to set a fore and back color. - - .PARAMETER DataGridView - Specifies the DataGridView Control to use - - .PARAMETER RowBackColor - Specifies the back color of the row to use - - .PARAMETER RowForeColor - Specifies the fore color of the row to use - - .PARAMETER SelectCell - Specifies to select only the cell when the value is found - - .PARAMETER SelectRow - Specifies to select the entire row when the value is found - - .PARAMETER Value - Specifies the value to search - - .EXAMPLE - PS C:\> Find-DataGridViewValue -DataGridView $datagridview1 -Value $textbox1.Text - - This will find the value and select the cell(s) - - .EXAMPLE - PS C:\> Find-DataGridViewValue -DataGridView $datagridview1 -Value $textbox1.Text -RowForeColor 'Red' -RowBackColor 'Black' - - This will find the value and color the fore and back of the row - .EXAMPLE - PS C:\> Find-DataGridViewValue -DataGridView $datagridview1 -Value $textbox1.Text -SelectRow - - This will find the value and select the entire row - - .NOTES - Francois-Xavier Cat - @lazywinadm - www.lazywinadmin.com -#> - [CmdletBinding(DefaultParameterSetName = "Cell")] - PARAM ( - [Parameter(Mandatory = $true)] - [System.Windows.Forms.DataGridView]$DataGridView, - - [Parameter(Mandatory = $true)] - $Value, - [Parameter(ParameterSetName = "Cell")] - [Switch]$SelectCell, - - [Parameter(ParameterSetName = "Row")] - [Switch]$SelectRow, - - #[Parameter(ParameterSetName = "Column")] - #[Switch]$SelectColumn, - - [Parameter(ParameterSetName = "RowColor")] - [system.Drawing.Color]$RowForeColor, - - [Parameter(ParameterSetName = "RowColor")] - [system.Drawing.Color]$RowBackColor - ) - BEGIN - { - Add-Type -AssemblyName System.Windows.Forms - } - PROCESS - { - FOR ([int]$i = 0; $i -lt $DataGridView.RowCount; $i++) - { - FOR ([int] $j = 0; $j -lt $DataGridView.ColumnCount; $j++) - { - $CurrentCell = $dataGridView.Rows[$i].Cells[$j] - - if ((-not $CurrentCell.Value.Equals([DBNull]::Value)) -and ($CurrentCell.Value.ToString() -like "*$Value*")) - { - # Row Selection - IF ($PSBoundParameters['SelectRow']) - { - $dataGridView.Rows[$i].Selected = $true - } - - <# - # Column Selection - IF ($PSBoundParameters['SelectColumn']) - { - #$DataGridView.Columns[$($CurrentCell.ColumnIndex)].Selected = $true - #$DataGridView.Columns[$j].Selected = $true - #$CurrentCell.DataGridView.Columns[$j].Selected = $true - } - #> - - # Row Fore Color - IF ($PSBoundParameters['RowForeColor']) - { - $dataGridView.Rows[$i].DefaultCellStyle.ForeColor = $RowForeColor - } - # Row Back Color - IF ($PSBoundParameters['RowBackColor']) - { - $dataGridView.Rows[$i].DefaultCellStyle.BackColor = $RowBackColor - } - - # Cell Selection - ELSEIF (-not ($PSBoundParameters['SelectRow']) -and -not ($PSBoundParameters['SelectColumn'])) - { - $CurrentCell.Selected = $true - } - }#IF not empty and contains value - }#For Each column - }#For Each Row - }#PROCESS -}#Find-DataGridViewValue + + function Find-DataGridViewValue { + # https://github.com/lazywinadmin/WinFormPS/blob/master/WinFormPS.psm1 + <# + .SYNOPSIS + The Find-DataGridViewValue function helps you to find a specific value and select the cell, row or to set a fore and back color. + + .DESCRIPTION + The Find-DataGridViewValue function helps you to find a specific value and select the cell, row or to set a fore and back color. + + .PARAMETER DataGridView + Specifies the DataGridView Control to use + + .PARAMETER RowBackColor + Specifies the back color of the row to use + + .PARAMETER RowForeColor + Specifies the fore color of the row to use + + .PARAMETER SelectCell + Specifies to select only the cell when the value is found + + .PARAMETER SelectRow + Specifies to select the entire row when the value is found + + .PARAMETER FindingColumns + Specifies the column(s) to search Value or NotValue + + .PARAMETER Value + Specifies the value to search + + .PARAMETER NotValue + Specifies the value to not match in all column (param FindingColumns is recomanded) + + .EXAMPLE + PS C:\> Find-DataGridViewValue -DataGridView $datagridview1 -Value $textbox1.Text + + This will find the value and select the cell(s) + + .EXAMPLE + PS C:\> Find-DataGridViewValue -DataGridView $datagridview1 -Value $textbox1.Text -RowForeColor 'Red' -RowBackColor 'Black' + + This will find the value and color the fore and back of the row + .EXAMPLE + PS C:\> Find-DataGridViewValue -DataGridView $datagridview1 -Value $textbox1.Text -SelectRow + + This will find the value and select the entire row + + .NOTES + Francois-Xavier Cat + @lazywinadm + www.lazywinadmin.com + #> + [CmdletBinding(DefaultParameterSetName = "Cell")] + PARAM ( + [ValidateNotNull()] + [Parameter(Mandatory = $true)] + [System.Windows.Forms.DataGridView]$DataGridView, + $Value, + $NotValue, + [string[]]$FindingColumns, + [Parameter(ParameterSetName = "Cell")] + [Switch]$SelectCell, + + [Parameter(ParameterSetName = "Row")] + [Switch]$SelectRow, + + #[Parameter(ParameterSetName = "Column")] + #[Switch]$SelectColumn, + [Parameter(ParameterSetName = "RowColor")] + [system.Drawing.Color]$RowForeColor, + [Parameter(ParameterSetName = "RowColor")] + [system.Drawing.Color]$RowBackColor + ) + + PROCESS + { + $DataGridView.ClearSelection() + ForEach ($Col in $DataGridView.Columns) { + if ($FindingColumns -contains $Col.Name -or !$FindingColumns) { + ForEach ($Row in $DataGridView.Rows) { + $CurrentCell = $dataGridView.Rows[$Row.index].Cells[$Col.index] + + if ((-not $CurrentCell.Value.Equals([DBNull]::Value)) -and ( ($Value -and ($CurrentCell.Value.ToString() -like "$Value")) -or ($NotValue -and ($CurrentCell.Value.ToString() -notlike "$NotValue")) )) + { + # Append-RichtextboxStatus -ComputerName $textboxSocieteName.Text -Source "Find $Value$NotValue" -Message "Colonne:$($col.name) ligne:$($row.index)" + # Row Selection + IF ($PSBoundParameters['SelectRow']) + { + $dataGridView.Rows[$Row.index].Selected = $true + } + + # Row Fore Color + IF ($PSBoundParameters['RowForeColor']) + { + $dataGridView.Rows[$Row.index].DefaultCellStyle.ForeColor = $RowForeColor + } + + # Row Back Color + IF ($PSBoundParameters['RowBackColor']) + { + $dataGridView.Rows[$Row.index].DefaultCellStyle.BackColor = $RowBackColor + } + # Cell Selection + ELSEIF (-not ($PSBoundParameters['SelectRow']) -and -not ($PSBoundParameters['RowForeColor']) -and -not ($PSBoundParameters['SelectColumn'])) + { + $CurrentCell.Selected = $true + # $CurrentCell.Selected() + # $CurrentCell.focus = $True + # $CurrentCell.focus() + } + }#IF not empty and contains value + } + } + } + }#PROCESS + }#Find-DataGridViewValue + function Get-Form { @@ -2014,4 +2013,4 @@ function Sort-ListViewColumn }#Sort-ListViewColumn # Export all the functions -Export-ModuleMember -Function * \ No newline at end of file +Export-ModuleMember -Function *