Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refact Find-DataGridViewValue() #1

Open
sctfic opened this issue Feb 22, 2015 · 3 comments
Open

refact Find-DataGridViewValue() #1

sctfic opened this issue Feb 22, 2015 · 3 comments
Assignees

Comments

@sctfic
Copy link

sctfic commented Feb 22, 2015

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
@lazywinadmin lazywinadmin self-assigned this Feb 23, 2015
@lazywinadmin
Copy link
Owner

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

@sctfic
Copy link
Author

sctfic commented Feb 23, 2015

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

@lazywinadmin
Copy link
Owner

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! 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants