Public/Add-WFDataGridViewColumn.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
function Add-WFDataGridViewColumn
{
<#
.SYNOPSIS
    This function add a column to a DataGridView
.DESCRIPTION
    This function add a column to a DataGridView
.EXAMPLE
    Add-WFDataGridViewColumn -DataGridView $datagridview1 -ColumnName $($textbox1.Text -split "," -replace '^\s+', '' -replace '\s+$', '')
.EXAMPLE
    Add-WFDataGridViewColumn -DataGridView $datagridview1 -ColumnName "First Name","Last Name","SamAccountName","Description"
.NOTES
    Francois-Xavier Cat
    www.lazywinadmin.com
    @lazywinadm
    github.com/lazywinadmin
#>

    [CmdletBinding()]
    PARAM (
        [ValidateNotNull()]
        [Parameter(Mandatory = $true)]
        [System.Windows.Forms.DataGridView]$DataGridView,
        
        [String[]]$ColumnName
    )
    BEGIN
    {
        Add-Type -AssemblyName System.Windows.Forms
    }
    PROCESS
    {
        foreach ($Column in $ColumnName)
        {
            
            # Create Column object
            $NewColumn = New-Object -TypeName System.Windows.Forms.DataGridViewTextBoxColumn
            $NewColumn.Name = $Column
            $NewColumn.HeaderText = $Column
            
            # Add the Column to the Datagridview
            $DataGridView.Columns.Add($NewColumn)
        }
    }
}