Public/Find-WFDataGridViewValue.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
function Find-WFDataGridViewValue
{
<#
    .SYNOPSIS
        The Find-WFDataGridViewValue function helps you to find a specific value and select the cell, row or to set a fore and back color.
     
    .DESCRIPTION
        The Find-WFDataGridViewValue 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-WFDataGridViewValue -DataGridView $datagridview1 -Value $textbox1.Text
     
        This will find the value and select the cell(s)
     
    .EXAMPLE
        PS C:\> Find-WFDataGridViewValue -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-WFDataGridViewValue -DataGridView $datagridview1 -Value $textbox1.Text -SelectRow
     
        This will find the value and select the entire row
     
    .NOTES
        Francois-Xavier Cat
        @lazywinadm
        www.lazywinadmin.com
        github.com/lazywinadmin
#>

    [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
}