突出显示datagridview上$ textbox.text的匹配出现

时间:2017-07-03 17:43:49

标签: datagridview highlight

我目前使用以下内容过滤数据网格视图:

$txtprod_TextChanged={

$ datagridtrousses.DataSource.DefaultView.RowFilter =“Produit LIKE' $($ txtprod.Text)'”

是否可以在powershell

中完全编写脚本(见图)

我有一个datagridview,我想突出显示我在datagridview中返回的字符串

Search matching occurences

由于

1 个答案:

答案 0 :(得分:0)

#------------------------------------------------------------------------
# Source File Information (DO NOT MODIFY)
# Source ID: 6beebf81-a3f9-47c7-9c98-ae965a4a8aad
# Source File: HIGHLIGHT DATAGRIDVIEW.psf
#------------------------------------------------------------------------
#region File Recovery Data (DO NOT MODIFY)
<#RecoveryData:
YQsAAB+LCAAAAAAABAC9VmFv2jAQ/T5p/8HK5wgIIYVKIRINtLDRFQGCfUNOcoA3x64cp8B+/RwC
HS1pEyZACJTEd/fe+b1zsIfg8xcQmzaWGKmLiHDW1KolQ3O+fkHIfhJkQRim94TCDxyC0+09dPvq
O0bt1rj1MOy1J73OtPQcze3yUXBawvsFvkRy8wxNbbSJJISlKWEBX0Wley7C9FdHWUs6muw41UqV
5KMjN6YyFtBkEEuBqY4GsUeJ/x02Y/4bWNOr17HlWzfGrVmDSuNWQ0xRaWpzVc/QkL8kNBAqTnM5
k4LTKO1UER0I/gxCbnYJrVjykY8ptEkILCGhQm90ZJh2eR+al/rIA9Cce4WUm+NSAkyOyB+VYFUb
OjKt29ykZJc1Z9tabuwY1jLhIsLj0M6Lwt7F9TkOdjVnybVd3q7uQ/Pl7GMP6MX1pAnKqNMaut3p
Y+/p7lvHHY8+0/cDmbYbPhYxHO3KcUKf+1gq9ppTNXV0LM9HAmVyLZCdkjMSN2S4LkNi7PVYAGvF
r0j01hApLaR4oY+Iqcneil7cAknpO76+uAmkwvH4+tO5zpPRrBfWcQ9XXLqaaq5aOUm6zOqHA5ps
rrvEbAHBP0qzg6fvJvZk9ZKXwYMgwYTA6uISBgpsocBeFNiJOrqcxiHrAg4UmS6QxXJ7fqaH7n64
Tx7rRpFJS/3whnuBrCFf7dhOSSCXSusi1kitVGvUlJVuTjsGMssfeskFSgeYMEnY4l0/s8O1Tyz1
9qYVRRAq4SHax+6ebJww8rmgxDuDeezya9X3KKldr4Fx9pHIR0wm8zpAAq+U7P+DVTHn1rw+N4zA
qmAT52P9DOlVenK5gPMDvd6mlrfLh3+pnb++5PXxYQsAAA==#>
#endregion

<#
    .NOTES
    --------------------------------------------------------------------------------
     Code generated by:  SAPIEN Technologies, Inc., PowerShell Studio 2017 v5.4.136
     Generated on:       2017-07-17 09:47
     Generated by:       Admin
    --------------------------------------------------------------------------------
    .DESCRIPTION
        GUI script generated by PowerShell Studio 2017
#>


#----------------------------------------------
#region Application Functions
#----------------------------------------------

#endregion Application Functions

#----------------------------------------------
# Generated Form Function
#----------------------------------------------
function Show-HIGHLIGHT_DATAGRIDVIEW_psf {

    #----------------------------------------------
    #region Import the Assemblies
    #----------------------------------------------
    [void][reflection.assembly]::Load('System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089')
    [void][reflection.assembly]::Load('System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089')
    [void][reflection.assembly]::Load('System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a')
    #endregion Import Assemblies

    #----------------------------------------------
    #region Generated Form Objects
    #----------------------------------------------
    [System.Windows.Forms.Application]::EnableVisualStyles()
    $form1 = New-Object 'System.Windows.Forms.Form'
    $labelSEARCHWMIOBJECTS = New-Object 'System.Windows.Forms.Label'
    $textbox1 = New-Object 'System.Windows.Forms.TextBox'
    $datagridview1 = New-Object 'System.Windows.Forms.DataGridView'
    $InitialFormWindowState = New-Object 'System.Windows.Forms.FormWindowState'
    #endregion Generated Form Objects

    #----------------------------------------------
    # User Generated Script
    #----------------------------------------------

    $form1_Load = {

        #TODO: Initialize Form Controls here
        $CACA = Get-WmiObject -List | Select-Object pscomputername, name
        Update-DataGridView -DataGridView $datagridview1 -Item $CACA
        $table = ConvertTo-DataTable -InputObject $CACA
        Load-DataGridView -DataGridView $datagridview1 -Item $table
        $datagridview1.Columns[0].MinimumWidth = 100
        $datagridview1.Columns[1].Width = 350
        #$datagridview1.Columns[2].Width = 175
    }

    #region Control Helper Functions
    function Update-DataGridView
    {
        <#
        .SYNOPSIS
            This functions helps you load items into a DataGridView.

        .DESCRIPTION
            Use this function to dynamically load items into the DataGridView control.

        .PARAMETER  DataGridView
            The DataGridView control you want to add items to.

        .PARAMETER  Item
            The object or objects you wish to load into the DataGridView's items collection.

        .PARAMETER  DataMember
            Sets the name of the list or table in the data source for which the DataGridView is displaying data.

        .PARAMETER AutoSizeColumns
            Resizes DataGridView control's columns after loading the items.
        #>
        Param (
            [ValidateNotNull()]
            [Parameter(Mandatory=$true)]
            [System.Windows.Forms.DataGridView]$DataGridView,
            [ValidateNotNull()]
            [Parameter(Mandatory=$true)]
            $Item,
            [Parameter(Mandatory=$false)]
            [string]$DataMember,
            [System.Windows.Forms.DataGridViewAutoSizeColumnMode]$AutoSizeColumns = 'None'
        )
        $DataGridView.SuspendLayout()
        $DataGridView.DataMember = $DataMember

        if ($Item -is [System.Data.DataSet] -and $Item.Tables.Count -gt 0)
        {
            $DataGridView.DataSource = $Item.Tables[0]
        }
        elseif ($Item -is [System.ComponentModel.IListSource]`
        -or $Item -is [System.ComponentModel.IBindingList] -or $Item -is [System.ComponentModel.IBindingListView] )
        {
            $DataGridView.DataSource = $Item
        }
        else
        {
            $array = New-Object System.Collections.ArrayList

            if ($Item -is [System.Collections.IList])
            {
                $array.AddRange($Item)
            }
            else
            {
                $array.Add($Item)
            }
            $DataGridView.DataSource = $array
        }

        if ($AutoSizeColumns -ne 'None')
        {
            $DataGridView.AutoResizeColumns($AutoSizeColumns)
        }

        $DataGridView.ResumeLayout()
    }

    function ConvertTo-DataTable
    {
        <#
            .SYNOPSIS
                Converts objects into a DataTable.

            .DESCRIPTION
                Converts objects into a DataTable, which are used for DataBinding.

            .PARAMETER  InputObject
                The input to convert into a DataTable.

            .PARAMETER  Table
                The DataTable you wish to load the input into.

            .PARAMETER RetainColumns
                This switch tells the function to keep the DataTable's existing columns.

            .PARAMETER FilterWMIProperties
                This switch removes WMI properties that start with an underline.

            .EXAMPLE
                $DataTable = ConvertTo-DataTable -InputObject (Get-Process)
        #>
        [OutputType([System.Data.DataTable])]
        param(
        [ValidateNotNull()]
        $InputObject, 
        [ValidateNotNull()]
        [System.Data.DataTable]$Table,
        [switch]$RetainColumns,
        [switch]$FilterWMIProperties)

        if($null -eq $Table)
        {
            $Table = New-Object System.Data.DataTable
        }

        if ($InputObject -is [System.Data.DataTable])
        {
            $Table = $InputObject
        }
        elseif ($InputObject -is [System.Data.DataSet] -and $InputObject.Tables.Count -gt 0)
        {
            $Table = $InputObject.Tables[0]
        }
        else
        {
            if (-not $RetainColumns -or $Table.Columns.Count -eq 0)
            {
                #Clear out the Table Contents
                $Table.Clear()

                if ($null -eq $InputObject) { return } #Empty Data

                $object = $null
                #find the first non null value
                foreach ($item in $InputObject)
                {
                    if ($null -ne $item)
                    {
                        $object = $item
                        break
                    }
                }

                if ($null -eq $object) { return } #All null then empty

                #Get all the properties in order to create the columns
                foreach ($prop in $object.PSObject.Get_Properties())
                {
                    if (-not $FilterWMIProperties -or -not $prop.Name.StartsWith('__')) #filter out WMI properties
                    {
                        #Get the type from the Definition string
                        $type = $null

                        if ($null -ne $prop.Value)
                        {
                            try { $type = $prop.Value.GetType() }
                            catch { Out-Null }
                        }

                        if ($null -ne $type) # -and [System.Type]::GetTypeCode($type) -ne 'Object')
                        {
                            [void]$table.Columns.Add($prop.Name, $type)
                        }
                        else #Type info not found
                        {
                            [void]$table.Columns.Add($prop.Name)
                        }
                    }
                }

                if ($object -is [System.Data.DataRow])
                {
                    foreach ($item in $InputObject)
                    {
                        $Table.Rows.Add($item)
                    }
                    return @( ,$Table)
                }
            }
            else
            {
                $Table.Rows.Clear()
            }

            foreach ($item in $InputObject)
            {
                $row = $table.NewRow()

                if ($item)
                {
                    foreach ($prop in $item.PSObject.Get_Properties())
                    {
                        if ($table.Columns.Contains($prop.Name))
                        {
                            $row.Item($prop.Name) = $prop.Value
                        }
                    }
                }
                [void]$table.Rows.Add($row)
            }
        }

        return @(,$Table)   
    }
    #endregion

    $textbox1_TextChanged = {

        $dataGridView1.Refresh()
        $datagridview1.DataSource.DefaultView.RowFilter = "name LIKE '*$($textbox1.Text)*'"


    }


    function Load-DataGridView
    {
        <#
        .SYNOPSIS
            This functions helps you load items into a DataGridView.

        .DESCRIPTION
            Use this function to dynamically load items into the DataGridView control.

        .PARAMETER  DataGridView
            The ComboBox control you want to add items to.

        .PARAMETER  Item
            The object or objects you wish to load into the ComboBox's items collection.

        .PARAMETER  DataMember
            Sets the name of the list or table in the data source for which the DataGridView is displaying data.

        #>
        Param (
            [ValidateNotNull()]
            [Parameter(Mandatory = $true)]
            [System.Windows.Forms.DataGridView]$DataGridView,
            [ValidateNotNull()]
            [Parameter(Mandatory = $true)]
            $Item,
            [Parameter(Mandatory = $false)]
            [string]$DataMember
        )
        $DataGridView.SuspendLayout()
        $DataGridView.DataMember = $DataMember

        if ($Item -is [System.ComponentModel.IListSource]`
            -or $Item -is [System.ComponentModel.IBindingList] -or $Item -is [System.ComponentModel.IBindingListView])
        {
            $DataGridView.DataSource = $Item
        }
        else
        {
            $array = New-Object System.Collections.ArrayList

            if ($Item -is [System.Collections.IList])
            {
                $array.AddRange($Item)
            }
            else
            {
                $array.Add($Item)
            }
            $DataGridView.DataSource = $array
        }

        $DataGridView.ResumeLayout()
    }

    $datagridview1_CellPainting = [System.Windows.Forms.DataGridViewCellPaintingEventHandler]{


        if ($_.RowIndex -ge 0 -and $_.ColumnIndex -ge 0 -and $textbox1.Text -ne "")
        {
            $_.Handled = $true
            $_.PaintBackground($_.CellBounds, $true)

            if ($sw = $textbox1.Text)
            {
                [string]$val = $_.FormattedValue
                [int]$sindx = $val.ToLower().IndexOf($sw.ToLower())
                [int]$sCount = 1
                while ($sindx -ge 0)
                {
                    $hl_rect = New-Object System.Drawing.Rectangle
                    $hl_rect.Y = $_.CellBounds.Y + 2
                    $hl_rect.Height = $_.CellBounds.Height - 5

                    $sBefore = $val.Substring(0, $sindx)
                    $sWord = $val.Substring($sindx, $sw.Length)
                    $s1 = [System.Windows.Forms.TextRenderer]::MeasureText($_.Graphics, $sBefore, $_.CellStyle.Font, $_.CellBounds.Size)
                    $s2 = [System.Windows.Forms.TextRenderer]::MeasureText($_.Graphics, $sWord, $_.CellStyle.Font, $_.CellBounds.Size)

                    if ($s1.Width -gt 5)
                    {
                        $hl_rect.X = $_.CellBounds.X + $s1.Width - 5
                        $hl_rect.Width = $s2.Width - 6
                    }
                    else
                    {
                        $hl_rect.X = $_.CellBounds.X + 2
                        $hl_rect.Width = $s2.Width - 6
                    }

                        $hl_brush = new-object System.Drawing.SolidBrush Yellow


                    $_.Graphics.FillRectangle($hl_brush, $hl_rect)

                    $hl_brush.Dispose()
                    $sindx = $val.ToLower().IndexOf($sw.ToLower(), $sCount++)
                }

                $_.PaintContent($_.CellBounds)
            }

        }
    }

    # --End User Generated Script--
    #----------------------------------------------
    #region Generated Events
    #----------------------------------------------

    $Form_StateCorrection_Load=
    {
        #Correct the initial state of the form to prevent the .Net maximized form issue
        $form1.WindowState = $InitialFormWindowState
    }

    $Form_Cleanup_FormClosed=
    {
        #Remove all event handlers from the controls
        try
        {
            $textbox1.remove_TextChanged($textbox1_TextChanged)
            $datagridview1.remove_CellPainting($datagridview1_CellPainting)
            $form1.remove_Load($form1_Load)
            $form1.remove_Load($Form_StateCorrection_Load)
            $form1.remove_FormClosed($Form_Cleanup_FormClosed)
        }
        catch { Out-Null <# Prevent PSScriptAnalyzer warning #> }
    }
    #endregion Generated Events

    #----------------------------------------------
    #region Generated Form Code
    #----------------------------------------------
    $form1.SuspendLayout()
    #
    # form1
    #
    $form1.Controls.Add($labelSEARCHWMIOBJECTS)
    $form1.Controls.Add($textbox1)
    $form1.Controls.Add($datagridview1)
    $form1.AutoScaleDimensions = '6, 13'
    $form1.AutoScaleMode = 'Font'
    $form1.ClientSize = '528, 359'
    $form1.Name = 'form1'
    $form1.Text = 'Form'
    $form1.add_Load($form1_Load)
    #
    # labelSEARCHWMIOBJECTS
    #
    $labelSEARCHWMIOBJECTS.AutoSize = $True
    $labelSEARCHWMIOBJECTS.Location = '23, 9'
    $labelSEARCHWMIOBJECTS.Name = 'labelSEARCHWMIOBJECTS'
    $labelSEARCHWMIOBJECTS.Size = '128, 13'
    $labelSEARCHWMIOBJECTS.TabIndex = 2
    $labelSEARCHWMIOBJECTS.Text = 'SEARCH WMI OBJECTS'
    #
    # textbox1
    #
    $textbox1.Location = '23, 37'
    $textbox1.Name = 'textbox1'
    $textbox1.Size = '140, 20'
    $textbox1.TabIndex = 1
    $textbox1.add_TextChanged($textbox1_TextChanged)
    #
    # datagridview1
    #
    $datagridview1.ColumnHeadersHeightSizeMode = 'AutoSize'
    $datagridview1.Location = '23, 83'
    $datagridview1.Name = 'datagridview1'
    $datagridview1.RowHeadersWidth = 10
    $datagridview1.Size = '484, 263'
    $datagridview1.TabIndex = 0
    $datagridview1.add_CellPainting($datagridview1_CellPainting)
    $form1.ResumeLayout()
    #endregion Generated Form Code

    #----------------------------------------------

    #Save the initial state of the form
    $InitialFormWindowState = $form1.WindowState
    #Init the OnLoad event to correct the initial state of the form
    $form1.add_Load($Form_StateCorrection_Load)
    #Clean up the control events
    $form1.add_FormClosed($Form_Cleanup_FormClosed)
    #Show the Form
    return $form1.ShowDialog()

} #End Function

#Call the form
Show-HIGHLIGHT_DATAGRIDVIEW_psf | Out-Null