WinFormPS
WinFormPS copied to clipboard
refact 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 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 (
[ValidateNotNull()]
[Parameter(Mandatory = $true)]
[System.Windows.Forms.DataGridView]$DataGridView,
$Value,
[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 ($CurrentCell.Value.ToString() -like "*$Value*"))
{
Append-RichtextboxStatus -ComputerName $textboxSocieteName.Text -Source "find" -Message "$($dataGridView.Rows[$Row.index]) $($CurrentCell.Value.ToString()) >> $($row.index) - $($col.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
}
}#IF not empty and contains value
}
}
}
}#PROCESS
}#Find-DataGridViewValue
What is not working exactly ? I would not put a dependance on Append-RichTextBoxStatus
pardon, ca fonctionne tres bien, c'est ma version modifiee si ca peu te faire gagner quelques minute de dev...
Oh ok. Qu'est-ce que tu as rajoute ? Tu devrais plutot faire un fork du repo et faire un pull request pour proposer des mises a jour :-)
J'apprecie le geste! :+1: