WinFormPS icon indicating copy to clipboard operation
WinFormPS copied to clipboard

refact Find-DataGridViewValue()

Open sctfic opened this issue 10 years ago • 3 comments

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

sctfic avatar Feb 22 '15 22:02 sctfic

What is not working exactly ? I would not put a dependance on Append-RichTextBoxStatus

lazywinadmin avatar Feb 23 '15 20:02 lazywinadmin

pardon, ca fonctionne tres bien, c'est ma version modifiee si ca peu te faire gagner quelques minute de dev...

sctfic avatar Feb 23 '15 20:02 sctfic

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:

lazywinadmin avatar Feb 24 '15 00:02 lazywinadmin