Skip to content

refact Find-DataGridViewValue() #1

@sctfic

Description

@sctfic
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

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions