locked
AutoSize DataGridView in Form Display Issues RRS feed

  • Question

  • I have the following script I am working on to modify it to fit my purpose once I am complete with testing the format however when I run it the autosize property for DataGridView causes the form to display with dark gray areas on the bottom and right hand side.  Can someone please tell me how to get rid of these?

    EDIT:  I would also like the form to update/refresh and resize column3 based on the text that is typed into that field.

    
    [void][System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
    $Form = New-Object System.Windows.Forms.Form
    $Form.FormBorderStyle = 'Fixed3D'
    $Form.AutoScale = $True
    
    $Form.Text = "Get Group Managers"
    $Form.TopMost = $True
    $Form.MinimizeBox = $False
    $Form.MaximizeBox = $False
    $Form.SizeGripStyle = 'Auto'
    $Form.Opacity = 1.0
    
    $FormPanel = New-Object System.Windows.Forms.Panel
    $FormPanel.Dock = 'Fill'
    $FormPanel.AutoSize = $True
    $FormPanel.AutosizeMode = 'GrowAndShrink'
    
    $DataGridView = New-Object System.Windows.Forms.DataGridView
    $DataGridView.AllowUserToAddRows = $False
    $DataGridView.AllowUserToDeleteRows = $False
    $DataGridView.AutoSize = $True
    $DataGridView.BorderStyle = 'None'
    $DataGridView.AutoSizeColumnsMode = 'Fill'
    $DataGridView.margin = 0
    $DataGridView.padding = 0
    $DataGridView.dock = 'Fill'
    
    $Chk_Sub_Button = New-Object System.Windows.Forms.Button
    $Chk_Sub_Button.text = "Check/Submit"
    #$Chk_Sub_Button.size = New-Object System.Drawing.Size(15,33)
    $Chk_Sub_Button.Dock = 'Bottom'
    
    $ApprovalText = New-Object System.Windows.Forms.Label
    $ApprovalText.text = 'Please enter approval information Group Name -> Approval received by:'
    
    $Column1 = New-Object System.Windows.Forms.DataGridViewTextBoxColumn
    $Column1.AutoSizeMode = [System.Windows.Forms.DataGridViewAutoSizeColumnMode]::DisplayedCells
    $Column1.ReadOnly = $True
    $Column1.HeaderText = 'Group Name'
    
    $Column2 = New-Object System.Windows.Forms.DataGridViewComboBoxColumn
    $Column2.AutoSizeMode = [System.Windows.Forms.DataGridViewAutoSizeColumnMode]::DisplayedCells
    $Column2.items.add('Approved')
    $Column2.items.add('Denied')
    $Column2.HeaderText = 'Approval/Denial'
    
    $Column3 = New-Object System.Windows.Forms.DataGridViewTextBoxColumn
    #$Column3.AutoSizeMode = [System.Windows.Forms.DataGridViewAutoSizeColumnMode]::Fill
    $Column3.AutoSizeMode = [System.Windows.forms.DataGridViewAutoSizeColumnMode]::DisplayedCells
    $Column3.HeaderText = 'Approving Official'
    
    $DataGridView.Columns.Add($Column1)
    $DataGridView.Columns.Add($Column2)
    $DataGridView.Columns.Add($Column3)
    
    $1 = 1
    $2 = 2
    $3 = 3
    
    if($1 -eq 1){$DataGridView.Rows.Add('Test')}
    if($2 -eq 2){$DataGridView.Rows.Add('Test')}
    if($3 -eq 3){$DataGridView.Rows.Add('Test')}
    
    $DataGridView.AutoResizeColumns('DisplayedCells')
    $DataGridView.AutoGenerateColumns = $True
    
    $CellValueChangedCodeBlock = 
        {$Column3.AutoSizeMode = [System.Windows.forms.DataGridViewAutoSizeColumnMode]::DisplayedCells}
        
    #$DataGridView.Add_CellValueChanged($CellValueChangedCodeBlock)
    
    $FormPanel.Controls.Add($DataGridView)
    $Form.Controls.AddRange(($Chk_Sub_Button, $FormPanel))
    
    $Form.Add_Load({
        $Form.AutoSize = $True
        $Form.AutoSizeMode = 'GrowAndShrink'
    })
    
    $Form.ShowDialog() | Out-Null

    
    • Edited by djohnson627 Tuesday, May 3, 2016 4:08 PM Additional Information
    Tuesday, May 3, 2016 3:48 PM

Answers

  • Thanks again for all of the help.  Below is the 95% completed script if you'd care to review it and or comment.  I know the buttons are out of whack when the form height resizes and I believe creating a panel and anchoring/docking the buttons within that panel will correct the issue.
    #------------------------------------------------------------------------
    # Source File Information (DO NOT MODIFY)
    # Source ID: 49b20ef8-692d-44a7-bba2-bc5083e808ad
    # Source File: Test3.psf
    #------------------------------------------------------------------------
    #region File Recovery Data (DO NOT MODIFY)
    <#RecoveryData:
    OBkAAB+LCAAAAAAABADNWVtv6kYQfq/U/2D5oU8oQEgCqIDEpbmoyTkRUE7forU9wDbrXWu9DnF/
    fcdeE67BCwF6FCmCZb65fjOzhkYfXPEGMu4RRSx8EVLBm/al3fr1F8tqfJd0Qjlht5TBN+JDawih
    qlwE4bhR3PhMI5x/wFWWigNo2oM4VOBf/KDcE7Pw4lZIX/8vWNs+KlijzIGri1LyV7C6EVORhCaH
    SEnCCtZz5DDq/gnxULwCbzrVKrl2r2/K9coVlGp12+LoStMeo747KaLgiXAyAR+4si13SpknEWV3
    BVdSsFCHiW4/SxGAVHEGb7suBKoTKSX4XAal+jAGxLuQiTmpwCByfIrqi3Nlxbm2z7RHSgxcwuBJ
    eGC3HvgUJFVmMPovIoYyAmNxbQSTMWtzbzCVlL/mYrsEg2SG8WvhPeLvMor10KFcVqsFq1yu54IS
    hnSE9EAOVMwQeUvfwav0coFP5J36aKsj3hFEWJifuifK94UkHWC3ttAuFzmYitmDm+TZzFKStztJ
    gywN99QzwCgi1bMIqaKJoS76hXl0JQDPxQ7hXaX8iQJrV1h/vOFxhnkUxNuajZfkk0YxlZ0D80eG
    JuLJh8YqnT+fFlt6jbtTIe1WR6AGfyM3m4AeJUxM+hCiq1iP1KQB7FG4RJewXL8pWPVLA4xm5nJs
    BiDdnNXrgnVZMRAfEueBe4DdYuKRZtRvxA9+N3borxBGNIwIS2nfIe5rV7Ak5Vtn4RodceC4r6s5
    eEnP1phY1FT8SYk53zOHE7NgPcJ4s3V38axyAMu0o6dkWXk/lhk7dDyWaZPHYVlyN8OJ740ozE7O
    td7daE+KMSZmmDg5FG3P66NPn22yndgeMFBwMBxHKZIJqxX5/Csa9nQgu2NldvVVq0fDgJEYvC4w
    Fu6hJbF9oIqVm9E3wU2c107fA0FkeA90MlWL+yJ6QBwGaVIonxirWyIKijwgU02ZnfQrXri0mtPR
    fMm9LTfa9LqyuMrqfk2iODSqrvAd8b+H1Q4CKd4IO2ZkP0W9sHVwv1EV74xs341nvvBwWhrIYmNn
    bYbbhTrp04vhhFlAf1BPTb/SoHjfF4x1iAyNR0T2jHZTK1i1q702dCl3W+JkS5YLnmSLE3P5sn66
    ujq3qBgRRj2iwFvgP452gvW41yj9eseaXn2z38Y+U5+sDDCjrwRylsw6WNNw+WkwaQHDB+MUkCvb
    RxPfOYsNv+FIy0bSdjKSTzvIbm3cZ79S3XPN9/VBfuoKzw1ZA0VUlI/VZZ6jTlW6Wu2IpTtvYy42
    lVHpbinLz+LWgn0YMq3ZJ/KrqW2HIfgYKIRzZdlJ3PJDV0hGnSMkq1H80LpuRZfnHDaOToF8iwkt
    z2NIkhneFg6xVaqMr8fVcbnsXZdIheTb+ttnZ4mpKyQc39DHW035RnH5l5LWfyhei244GQAA#>
    #endregion
    
    <#
        .NOTES
        --------------------------------------------------------------------------------
         Code generated by:  SAPIEN Technologies, Inc., PowerShell Studio 2016 v5.2.120
         Generated on:       5/6/2016 4:56 AM
         Generated by:       Darius
        --------------------------------------------------------------------------------
        .DESCRIPTION
            GUI script generated by PowerShell Studio 2016
    #>
    #----------------------------------------------
    #region Application Functions
    #----------------------------------------------
    
    #endregion Application Functions
    
    #----------------------------------------------
    # Generated Form Function
    #----------------------------------------------
    function Call-Test3_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()
    	$formGroupManagement = New-Object 'System.Windows.Forms.Form'
    	$buttonCancel = New-Object 'System.Windows.Forms.Button'
    	$buttonSubmit = New-Object 'System.Windows.Forms.Button'
    	$DGV = New-Object 'System.Windows.Forms.DataGridView'
    	$Group = New-Object 'System.Windows.Forms.DataGridViewTextBoxColumn'
    	$Approval = New-Object 'System.Windows.Forms.DataGridViewComboBoxColumn'
    	$Authority = New-Object 'System.Windows.Forms.DataGridViewTextBoxColumn'
    	$InitialFormWindowState = New-Object 'System.Windows.Forms.FormWindowState'
    	#endregion Generated Form Objects
    
    	#----------------------------------------------
    	# User Generated Script
    	#----------------------------------------------
    	
    	$formGroupManagement_Load={
    		#TODO: Initialize Form Controls here
    		
    		if ($1 = 1) { $DGV.Rows.Add('Test1') }
    		
    		if ($2 = 2) { $DGV.Rows.Add('Test2') }
    		
    		if ($3 = 3) { $DGV.Rows.Add('Test3') }
    		
    		$formGroupManagement.width = $DGV.width
            $DGV.Width = ($DGV.Width + 46)
    		foreach ($row in $DGV.Rows) { ($totalheight += $row.height) }
    		$DGV.Height = $totalheight + $DGV.ColumnHeadersHeight
    		$DGV.Height = ($DGV.ColumnHeadersHeight + ($DGV.Rows.GetRowsHeight('Displayed')) + 2)
    		
    	}
    	
    	#region Control Helper Functions
    	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 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.
    	
    		#>
    		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()
    	}
    	
    	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($Table -eq $null)
    		{
    			$Table = New-Object System.Data.DataTable
    		}
    	
    		if($InputObject-is [System.Data.DataTable])
    		{
    			$Table = $InputObject
    		}
    		else
    		{
    			if(-not $RetainColumns -or $Table.Columns.Count -eq 0)
    			{
    				#Clear out the Table Contents
    				$Table.Clear()
    	
    				if($InputObject -eq $null){ return } #Empty Data
    				
    				$object = $null
    				#find the first non null value
    				foreach($item in $InputObject)
    				{
    					if($item -ne $null)
    					{
    						$object = $item
    						break	
    					}
    				}
    	
    				if($object -eq $null) { 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($prop.Value -ne $null)
    						{
    							try{ $type = $prop.Value.GetType() } catch {}
    						}
    	
    						if($type -ne $null) # -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
    	
    	$buttonCancel_Click={
    		#TODO: Place custom script here
    		$formGroupManagement.Close()
    	}
    	
    	$DGV_CellValidated=[System.Windows.Forms.DataGridViewCellEventHandler]{
    	#Event Argument: $_ = [System.Windows.Forms.DataGridViewCellEventArgs]
    		#TODO: Place custom script here
    		$formGroupManagement.AutoSize = $true
    		$formGroupManagement.AutoSizeMode = 'GrowAndShrink'
    		$DGV.AutoSize = $true
    		$DGV.AutoSizeRowsMode = 'AllCells'
    		$Authority.AutoSizeMode = 'AllCells'
    		#$Authority.AutoSizeMode = 'Fill'
    		$formGroupManagement.width = $DGV.width
            $DGV.Width = ($DGV.Width - 15)
            foreach($row in $DGV.Rows){($totalHeight += $row.height)}
            $DGV.Height = $totalHeight + $DGV.ColumnHeadersHeight
            $DGV.Height = ($DGV.ColumnHeadersHeight + ($DGV.Rows.GetRowsHeight('Displayed')) + 2)
            $formGroupManagement.AutoSize = $False
            $DGV.Dock = 'Fill'
            #$formGroupManagement.AutoSize = $true
    	}
    	
    	$DGV_CellContentClick=[System.Windows.Forms.DataGridViewCellEventHandler]{
    	#Event Argument: $_ = [System.Windows.Forms.DataGridViewCellEventArgs]
    		#TODO: Place custom script here
    		$formGroupManagement.AutoSize = $true
    		$formGroupManagement.AutoSizeMode = 'GrowAndShrink'
    		$DGV.AutoSize = $true
    		$DGV.AutoSizeRowsMode = 'AllCells'
    		$Authority.AutoSizeMode = 'AllCells'
    		#$Authority.AutoSizeMode = 'Fill'
            $formGroupManagement.width = $DGV.width
            $DGV.Width = ($DGV.Width - 15)
            foreach($row in $DGV.Rows){($totalHeight += $row.height)}
            $DGV.Height = $totalHeight + $DGV.ColumnHeadersHeight
            $DGV.Height = ($DGV.ColumnHeadersHeight + ($DGV.Rows.GetRowsHeight('Displayed')) + 2)
    	}
    	$buttonSubmit_Click={
    		#TODO: Place custom script here
            read-host $DGV.Rows[$_.RowIndex].Cells[$_.ColumnIndex].Value.ToString()
    	}
    	
    	$DGV_Resize={
    		#TODO: Place custom script here
    		$formGroupManagement.width = $DGV.width
    		foreach ($row in $DGV.Rows) { ($totalHeight += $row.height)}
            $DGV.Height = $totalHeight + $DGV.ColumnHeadersHeight
            $DGV.Height = ($DGV.ColumnHeadersHeight + ($DGV.Rows.GetRowsHeight('Displayed')) + 2)
    		
    	}
    	
    	# --End User Generated Script--
    	#----------------------------------------------
    	#region Generated Events
    	#----------------------------------------------
    	
    	$Form_StateCorrection_Load=
    	{
    		#Correct the initial state of the form to prevent the .Net maximized form issue
    		$formGroupManagement.WindowState = $InitialFormWindowState
    	}
    	
    	$Form_Cleanup_FormClosed=
    	{
    		#Remove all event handlers from the controls
    		try
    		{
    			$buttonCancel.remove_Click($buttonCancel_Click)
    			$buttonSubmit.remove_Click($buttonSubmit_Click)
    			$DGV.remove_CellContentClick($DGV_CellContentClick)
    			$DGV.remove_CellValidated($DGV_CellValidated)
    			$DGV.remove_Resize($DGV_Resize)
    			$formGroupManagement.remove_Load($formGroupManagement_Load)
    			$formGroupManagement.remove_Load($Form_StateCorrection_Load)
    			$formGroupManagement.remove_FormClosed($Form_Cleanup_FormClosed)
    		}
    		catch [Exception]
    		{ }
    	}
    	#endregion Generated Events
    
    	#----------------------------------------------
    	#region Generated Form Code
    	#----------------------------------------------
    	$formGroupManagement.SuspendLayout()
    	#
    	# formGroupManagement
    	#
    	$formGroupManagement.Controls.Add($buttonCancel)
    	$formGroupManagement.Controls.Add($buttonSubmit)
    	$formGroupManagement.Controls.Add($DGV)
    	$formGroupManagement.AcceptButton = $buttonSubmit
    	$formGroupManagement.AutoScaleMode = 'Inherit'
    	$formGroupManagement.AutoSize = $true
    	$formGroupManagement.AutoSizeMode = 'GrowAndShrink'
    	$formGroupManagement.CancelButton = $buttonCancel
    	$formGroupManagement.ClientSize = '277, 119'
    	$formGroupManagement.FormBorderStyle = 'Fixed3D' #makes form non-resizable via mouse controls
    	$formGroupManagement.MaximizeBox = $False
    	$formGroupManagement.MinimizeBox = $False
    	$formGroupManagement.Name = 'formGroupManagement'
    	$formGroupManagement.ShowIcon = $False
    	$formGroupManagement.SizeGripStyle = 'Hide'
    	$formGroupManagement.StartPosition = 'CenterScreen'
    	$formGroupManagement.Text = 'Group Management'
    	$formGroupManagement.add_Load($formGroupManagement_Load)
    	#
    	# buttonCancel
    	#
    	$buttonCancel.Anchor = 'Left'
    	$buttonCancel.DialogResult = 'Cancel'
    	$buttonCancel.Location = '196, 92'
    	$buttonCancel.Name = 'buttonCancel'
    	$buttonCancel.Size = '75, 23'
    	$buttonCancel.TabIndex = 2
    	$buttonCancel.Text = '&Cancel'
    	$buttonCancel.UseVisualStyleBackColor = $true
    	$buttonCancel.add_Click($buttonCancel_Click)
    	#
    	# buttonSubmit
    	#
    	$buttonSubmit.Anchor = 'Top'
    	$buttonSubmit.Location = '3, 92'
    	$buttonSubmit.Name = 'buttonSubmit'
    	$buttonSubmit.Size = '75, 23'
    	$buttonSubmit.TabIndex = 1
    	$buttonSubmit.Text = '&Submit'
    	$buttonSubmit.UseVisualStyleBackColor = $true
    	$buttonSubmit.add_Click($buttonSubmit_Click)
    	#
    	# DGV
    	#
    	$DGV.AllowUserToAddRows = $False
    	$DGV.AllowUserToDeleteRows = $False
    	$DGV.AllowUserToResizeColumns = $False
    	$DGV.AllowUserToResizeRows = $False
    	$DGV.AutoSizeColumnsMode = 'DisplayedCells'
    	$DGV.AutoSizeRowsMode = 'AllCells'
    	$DGV.BorderStyle = 'None'
    	$DGV.ColumnHeadersHeightSizeMode = 'DisableResizing'
    	[void]$DGV.Columns.Add($Group)
    	[void]$DGV.Columns.Add($Approval)
    	[void]$DGV.Columns.Add($Authority)
    	$DGV.Location = '3, 2'
    	$DGV.Name = 'DGV'
    	$DGV.RowHeadersVisible = $False
    	$DGV.RowHeadersWidthSizeMode = 'DisableResizing'
    	$DGV.ScrollBars = 'None'
    	$DGV.Size = '268, 84'
    	$DGV.TabIndex = 0
    	$DGV.add_CellContentClick($DGV_CellContentClick)
    	$DGV.add_CellValidated($DGV_CellValidated)
    	$DGV.add_Resize($DGV_Resize)
    	#
    	# Group
    	#
    	$Group.AutoSizeMode = 'DisplayedCells'
    	$Group.HeaderText = 'Group Name'
    	$Group.Name = 'Group'
    	$Group.ReadOnly = $true
    	$Group.Resizable = 'True'
    	$Group.Width = 92
    	#
    	# Approval
    	#
    	$Approval.AutoSizeMode = 'DisplayedCells'
    	$Approval.HeaderText = 'Approval Status'
    	$Approval.Name = 'Approval'
    	$Approval.Resizable = 'True'
    	$Approval.Width = 88
        $Approval.items.add('')
        $Approval.items.add('Approved')
        $Approval.items.add('Denied')
    	#
    	# Authority
    	#
    	$Authority.AutoSizeMode = 'Fill'
    	$Authority.HeaderText = 'Approval Authority'
    	$Authority.Name = 'Authority'
    	$formGroupManagement.ResumeLayout()
    	#endregion Generated Form Code
    
    	#----------------------------------------------
    
    	#Save the initial state of the form
    	$InitialFormWindowState = $formGroupManagement.WindowState
    	#Init the OnLoad event to correct the initial state of the form
    	$formGroupManagement.add_Load($Form_StateCorrection_Load)
    	#Clean up the control events
    	$formGroupManagement.add_FormClosed($Form_Cleanup_FormClosed)
    	#Show the Form
    	return $formGroupManagement.ShowDialog()
    
    } #End Function
    
    #Call the form
    Call-Test3_psf | Out-Null
    

    • Marked as answer by djohnson627 Friday, May 6, 2016 8:57 PM
    Friday, May 6, 2016 8:57 PM

All replies

  • You cannot have multiple objects all having 'Fill" as the docking mode.  Once a control is asked to 'Fill then the whole parent control will be filled.

    You also have an unused flowpanel which does not help any.


    \_(ツ)_/

    Tuesday, May 3, 2016 4:19 PM
  • JRV,

    Thanks for the tip.  I commented out the additional 'Fill' docking modes however that appears to have made things worse.  Now I have a light grey/white space on the right-hand side of the dgv and a dark grey space on the bottom before the resize occurs.  Once the resize occurs (after inputting text into column3 and selecting a different column), there is a dark grey space on both the right-hand side and bottom.  Additionally, I don't fully understand what a Flow Panel is or what needs to be done in order to make things work as I would like.

    Tuesday, May 3, 2016 4:33 PM
  • Start with this.  It sets up visual styles correctly and floats the DGV short of the bottom.  It also resizes correctly. 

    #region Import the Assemblies
    #----------------------------------------------
    Add-Type -AssemblyName System.Windows.Forms
    
    #endregion Import Assemblies
    
    #----------------------------------------------
    #region Generated Form Objects
    #----------------------------------------------
    [System.Windows.Forms.Application]::EnableVisualStyles()
    $form1 = New-Object 'System.Windows.Forms.Form'
    $datagridview1 = New-Object 'System.Windows.Forms.DataGridView'
    $buttonOK = New-Object 'System.Windows.Forms.Button'
    $Column1 = New-Object 'System.Windows.Forms.DataGridViewTextBoxColumn'
    $Column2 = New-Object 'System.Windows.Forms.DataGridViewTextBoxColumn'
    $Column3 = New-Object 'System.Windows.Forms.DataGridViewTextBoxColumn'
    $InitialFormWindowState = New-Object 'System.Windows.Forms.FormWindowState'
    #endregion Generated Form Objects
    
    #----------------------------------------------
    # User Generated Script
    #----------------------------------------------
    
    $form1_Load = {
    	#TODO: Initialize Form Controls here
    	
    }
    
    # --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 {
    		$form1.remove_Load($form1_Load)
    		$form1.remove_Load($Form_StateCorrection_Load)
    		$form1.remove_FormClosed($Form_Cleanup_FormClosed)
    	} catch [Exception]
    	{ }
    }
    #endregion Generated Events
    
    #----------------------------------------------
    #region Generated Form Code
    #----------------------------------------------
    $form1.SuspendLayout()
    #
    # form1
    #
    $form1.Controls.Add($datagridview1)
    $form1.Controls.Add($buttonOK)
    $form1.AcceptButton = $buttonOK
    $form1.AutoSize = $True
    $form1.ClientSize = '537, 311'
    $form1.MaximizeBox = $False
    $form1.MinimizeBox = $False
    $form1.Name = 'form1'
    $form1.StartPosition = 'CenterScreen'
    $form1.Text = 'Form'
    $form1.add_Load($form1_Load)
    #
    # datagridview1
    #
    $datagridview1.ColumnHeadersHeightSizeMode = 'AutoSize'
    [void]$datagridview1.Columns.Add($Column1)
    [void]$datagridview1.Columns.Add($Column2)
    [void]$datagridview1.Columns.Add($Column3)
    $datagridview1.Dock = 'Fill'
    $datagridview1.Location = '0, 0'
    $datagridview1.Margin = '0, 0, 0, 70'
    $datagridview1.Name = 'datagridview1'
    $datagridview1.Size = '537, 288'
    $datagridview1.TabIndex = 1
    #
    # buttonOK
    #
    $buttonOK.AutoSize = $True
    $buttonOK.AutoSizeMode = 'GrowAndShrink'
    $buttonOK.DialogResult = 'OK'
    $buttonOK.Dock = 'Bottom'
    $buttonOK.Location = '0, 288'
    $buttonOK.Name = 'buttonOK'
    $buttonOK.Size = '537, 23'
    $buttonOK.TabIndex = 0
    $buttonOK.Text = '&OK'
    $buttonOK.UseVisualStyleBackColor = $True
    #
    # Column1
    #
    $Column1.AutoSizeMode = 'Fill'
    $Column1.HeaderText = 'Column1'
    $Column1.Name = 'Column1'
    #
    # Column2
    #
    $Column2.AutoSizeMode = 'Fill'
    $Column2.HeaderText = 'Column2'
    $Column2.Name = 'Column2'
    #
    # Column3
    #
    $Column3.AutoSizeMode = 'Fill'
    $Column3.HeaderText = 'Column3'
    $Column3.Name = 'Column3'
    $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
    $form1.ShowDialog()
    


    \_(ツ)_/

    Tuesday, May 3, 2016 4:44 PM
  • JRV,

    Thanks a lot for the script.  I modified it as needed to fit my requirements and it almost does everything I require it to do.  The only remaining due out is that the form itself doesn't resize when the data in the DataGridView exceeds the current size of the form.  If this is not possible then I can work with it but I've been told to do everything possible to make this function a reality.

    Tuesday, May 3, 2016 6:37 PM
  • The form will not resize.  You will have to code the event to adjust the form size when the columns expand.


    \_(ツ)_/


    • Edited by jrv Tuesday, May 3, 2016 6:50 PM
    Tuesday, May 3, 2016 6:49 PM
  • JRV,

    I believe I found exactly what I need to have the form resize when a selected row changes.  I appreciate all of the help you have provided.  I've been banging my head against it for a few weeks.

    Tuesday, May 3, 2016 7:02 PM
  • JRV,

    I'm having trouble locating an event handler that I can use to resize the form on either a button click or selection change.  I have the information necessary to create a button click or selection change action but not the event handler to point the event to.

    Tuesday, May 3, 2016 7:47 PM
  • What event handler are you looking for? There are a great many on all controls.

    \_(ツ)_/

    Tuesday, May 3, 2016 7:50 PM
  • I've tried the following options I found using the Get-Member cmdlet (hopefully these are actually event handlers though I realize it is very likely I am wrong).

    set_Size

    set_ClientSize

    add_AutoSizeChanged

    add_Resize

    add_SizeChanged

    Tuesday, May 3, 2016 8:04 PM
  • What event is it that is sent when you make changes. That all depends on what causes the changes.


    \_(ツ)_/

    Tuesday, May 3, 2016 8:25 PM
  • Please see the code provided.  I believe these are event/event handler statements but I can't figure out what to put as the triggered event.

    $Chk_Sub_Btn.add_Click({$ApprovalForm.SetSize($DGV.Size)})
    
    $CellValueChangedCodeBlock = {
    $Authority.Autosizemode = [System.Windows.Forms.Form]::????}
    
    $DGV.Add_CellValueChanged($CellValueChangedCodeBlock)

    $Chk_Sub_Btn is the "OK" button, $DGV is the DataGridView.
    Tuesday, May 3, 2016 8:37 PM
  • Ok.  Now you get to learn how to write forms code.

    I think you question has been answered.

    I will suggest using the control class reference on MSDN for technical information.

    https://msdn.microsoft.com/en-us/library/mt468159(v=vs.110).aspx


    \_(ツ)_/

    Tuesday, May 3, 2016 8:44 PM
  • Ok, so I have the following code.  Can you at least tell me if I'm headed in the right direction to achieve my desired results?  From what I understand about event handlers the following is what is being used.

    Event = Add_CellValueChanged

    Object = $DGV

    Action = $CellValueChangedCodeBlock

    $CellValueChangedCodeBlock =
    {
    if($AuthorityColumn.width -gt 105){
    $ApprovalForm.Autosize = $True
    $AuthorityColumn.AutoSizeMode = [System.Windows.Forms.DataGridViewAutoSizeColumnMode]::Fill
    }
    }
    
    $DGV.Add_CellValueChanged($CellValueChangedCodeBlock)


    Wednesday, May 4, 2016 2:02 PM
  • As I posted above.  You will have yo explicitly set the sizes for everything when the size of a column exceeds the visible boundaries.

    \_(ツ)_/

    Wednesday, May 4, 2016 5:06 PM
  • You might do better using the autosize and catching the cell format changes event.

    $datagridview1_ColumnWidthChanged=[System.Windows.Forms.DataGridViewColumnEventHandler]{
    #Event Argument: $_ = [System.Windows.Forms.DataGridViewColumnEventArgs]
    	
    }
    


    \_(ツ)_/

    Wednesday, May 4, 2016 5:08 PM
  • A quick test shows that the only way to detect that the cell width has changed is in the cell validated event:

    $datagridview1_CellValidated=[System.Windows.Forms.DataGridViewCellEventHandler]{
    #Event Argument: $_ = [System.Windows.Forms.DataGridViewCellEventArgs]
    	Write-Host $datagridview1.Rows[$_.RowIndex].Cells[$_.ColumnIndex].Size.Width
    }


    \_(ツ)_/

    Wednesday, May 4, 2016 5:37 PM
  • Thanks.  Now I have the form doing almost exactly what I need it to.  Now I just have to get rid of the dark grey areas to the bottom and right of the DGV.  I probably should have prefaced the original question with how new to PS I am.  While there are some (very few) basics I understand there is a great deal I have no clue about as I am learning from internet research, reading books and trial and error.
    Wednesday, May 4, 2016 6:10 PM
  • My example does not produce that issue.  It is caused by not setting up visual styles correctly and by using a broken mix of controls.

    You can download a 45 day trial of PowerShell Studio here: Download Trial

    It will help you learn how this works.  Create and test you form then look at the generated script to see how it is done.


    \_(ツ)_/


    • Edited by jrv Wednesday, May 4, 2016 7:03 PM
    Wednesday, May 4, 2016 7:03 PM
  • I have included my updated script for review.  Unfortunately I am at work and not allowed to install software without prior authorization.  I tried to get authorization for the software you suggested a few weeks ago and was told no due to the licensing methods used.  I will have to try this at home then translate it to work when possible.

    #region Import the Assemblies
    #----------------------------------------------
    Add-Type -AssemblyName System.Windows.Forms
    
    #endregion Import Assemblies
    
    #----------------------------------------------
    #region Generated Form Objects
    #----------------------------------------------
    [System.Windows.Forms.Application]::EnableVisualStyles()
    $ApprovalForm = New-Object 'System.Windows.Forms.Form'
    $DGV = New-Object 'System.Windows.Forms.DataGridView'
    $Chk_Sub_Btn = New-Object 'System.Windows.Forms.Button'
    $Column1 = New-Object 'System.Windows.Forms.DataGridViewTextBoxColumn'
    $Column2 = New-Object 'System.Windows.Forms.DataGridViewTextBoxColumn'
    $Column3 = New-Object 'System.Windows.Forms.DataGridViewTextBoxColumn'
    $InitialFormWindowState = New-Object 'System.Windows.Forms.FormWindowState'
    #endregion Generated Form Objects
    
    #----------------------------------------------
    # User Generated Script
    #----------------------------------------------
    
    $ApprovalForm_Load = {
    	#TODO: Initialize Form Controls here
    $1 = 1
    $2 = 2
    $3 = 3
    #Will be replaced with variables imported from a different form/script
    if ($1 = 1 {DGV.Rows.Add('Test')}
    
    if ($2 = 3 {DGV.Rows.Add('Test')}
    
    if ($3 = 3 {DGV.Rows.Add('Test')}
    	
    $DGV_ColumnWidthChanged = {
    #Event Argument: $_ = [System.Windows.Forms.DataGridViewCellEventArgs]
    $ApprovalForm.AutoSizeChanged
    }
    
    $DGV.Add_CellValueChanged($DGV_ColumnWidthChanged)
    
    #Will be replaced with variables imported from a different form/script
    $Chk_Sub_Btn.Add_Click({})
    
    
    }
    
    # --End User Generated Script--
    #----------------------------------------------
    #region Generated Events
    #----------------------------------------------
    
    $ApprovalForm_StateCorrection_Load =
    {
    	#Correct the initial state of the form to prevent the .Net maximized form issue
    	$ApprovalForm.WindowState = $InitialFormWindowState
    }
    
    $ApprovalForm_Cleanup_FormClosed =
    {
    	#Remove all event handlers from the controls
    	try {
    		$ApprovalForm.remove_Load($ApprovalForm_Load)
    		$ApprovalForm.remove_Load($ApprovalForm_StateCorrection_Load)
    		$ApprovalForm.remove_FormClosed($Form_Cleanup_FormClosed)
    	} catch [Exception]
    	{ }
    }
    #endregion Generated Events
    
    #----------------------------------------------
    #region Generated Form Code
    #----------------------------------------------
    $ApprovalForm.SuspendLayout()
    #
    # form1
    #
    $ApprovalForm.Controls.Add($DGV)
    $ApprovalForm.Controls.Add($Chk_Sub_Btn)
    $ApprovalForm.AcceptButton = $Chk_Sub_Btn
    $ApprovalForm.AutoSize = $True
    $ApprovalForm.AutoSizeMode = 'GrowAndShrink'
    $ApprovalForm.ClientSize = '537, 311'
    $ApprovalForm.MaximizeBox = $False
    $ApprovalForm.MinimizeBox = $False
    $ApprovalForm.Name = 'Group Management Approval Documentation Form'
    $ApprovalForm.StartPosition = 'CenterScreen'
    $ApprovalForm.Text = 'Group Management Approval Documentation Form'
    $ApprovalForm.FormBorderStyle - 'Fixed3D'
    $ApprovalForm.add_Load($ApprovalForm_Load)
    #
    # DGV
    #
    $DGV.ColumnHeadersHeightSizeMode = 'AutoSize'
    [void]$DGV.Columns.Add($GroupNameColumn)
    [void]$DGV.Columns.Add($ApprovalColumn)
    [void]$DGV.Columns.Add($AuthorityColumn)
    $DGV.Dock = 'Fill'
    $DGV.Location = '0, 0'
    $DGV.Margin = '0, 0, 0, 70'
    $DGV.Name = 'GroupManagement'
    $DGV.Size = '537, 288'
    $DGV.AutoSize = $True
    $DGV.AutoSizeColumnsMode = 'DisplayedCells'
    $DGV.TabIndex = 1
    $DGV.AllowUserToAddRows = $False
    $DGV.AllowUserToDeleteRows = $False
    $DGV.AllowUserToResizeRows = $False
    $DGV.AllowUserToResizeColumns = $False
    $DGV.RowHeadersWidthSizeMode = 'DisableResizing'
    $DGV.BorderStyle = 'None'
    #
    # Chk_Sub_Btn
    #
    $Chk_Sub_Btn.AutoSize = $True
    $Chk_Sub_Btn.AutoSizeMode = 'GrowAndShrink'
    $Chk_Sub_Btn.DialogResult = 'OK'
    $Chk_Sub_Btn.Dock = 'Bottom'
    $Chk_Sub_Btn.Location = '0, 288'
    $Chk_Sub_Btn.Name = 'Check/Submit Button'
    $Chk_Sub_Btn.Size = '537, 23'
    $Chk_Sub_Btn.TabIndex = 0
    $Chk_Sub_Btn.Text = '&OK'
    $Chk_Sub_Btn.UseVisualStyleBackColor = $True
    #
    # Column1
    #
    $Column1.HeaderText = 'Group Name'
    $Column1.Name = 'Group Name Column'
    $GroupNameColumn.ReadOnly = $True
    #
    # Column2
    #
    $Column2.HeaderText = 'Approval Status'
    $Column2.Name = 'Approval Column'
    $ApprovalColumn.items.add('Approved')
    $ApprovalColumn.items.add('Denied')
    #
    # Column3
    #
    $Column3.HeaderText = 'Approval Authority'
    $Column3.Name = 'Authority Column'
    $ApprovalForm.ResumeLayout()
    #endregion Generated Form Code
    
    #----------------------------------------------
    
    #Save the initial state of the form
    $InitialFormWindowState = $ApprovalForm.WindowState
    #Init the OnLoad event to correct the initial state of the form
    
    $ApprovalForm.add_Load($ApprovalForm_StateCorrection_Load)
    #Clean up the control events
    $ApprovalForm.add_FormClosed($Form_Cleanup_FormClosed)
    #Show the Form
    $ApprovalForm.ShowDialog()

    Wednesday, May 4, 2016 7:42 PM
  • Just generate a PS1 and cart it to work for testing.  You can alos dump the modules to a file and take the AD and any other things home to get intellisense to work.

    \_(ツ)_/

    Wednesday, May 4, 2016 7:49 PM
  • I will definitely do that.  But for now if possible I would like to finish this task since it appears to be so close to being complete.
    Wednesday, May 4, 2016 7:54 PM
  • I have downloaded and installed the latest version of PowerShell Studio on my home notebook and began exploring the functionality last night.  While it is definitely a great tool and I may just purchase it for my home system to allow me to work on the few projects I can do from home, I have still been unable to identify the correct control/event handler to perform the task I require.  I rebuilt my form from scratch using the design view in PowerShell Studio 2016 and all other functionality is working even better than the script that was written out by hand.  I will continue to toy with the application until and even after this project is completed but was wondering if you could provide more insight as to which control/method/event handler/etc should be used to achieve my desired result.
    Thursday, May 5, 2016 11:55 AM
  • Every time a cell value is changed you will have to determine if all form and control sizes need to be reset.  There is no automatic way to do this.

    I would use CellValidated event to do this.

    If the cell width changes you can detect it here:

    $datagridview1_CellValidated=[System.Windows.Forms.DataGridViewCellEventHandler]{
    #Event Argument: $_ = [System.Windows.Forms.DataGridViewCellEventArgs]
    	Write-Host $datagridview1.Rows[$_.RowIndex].Cells[$_.ColumnIndex].Size.Width
    }
    


    \_(ツ)_/

    Thursday, May 5, 2016 5:40 PM
  • Thanks again for all of the help.  Below is the 95% completed script if you'd care to review it and or comment.  I know the buttons are out of whack when the form height resizes and I believe creating a panel and anchoring/docking the buttons within that panel will correct the issue.
    #------------------------------------------------------------------------
    # Source File Information (DO NOT MODIFY)
    # Source ID: 49b20ef8-692d-44a7-bba2-bc5083e808ad
    # Source File: Test3.psf
    #------------------------------------------------------------------------
    #region File Recovery Data (DO NOT MODIFY)
    <#RecoveryData:
    OBkAAB+LCAAAAAAABADNWVtv6kYQfq/U/2D5oU8oQEgCqIDEpbmoyTkRUE7forU9wDbrXWu9DnF/
    fcdeE67BCwF6FCmCZb65fjOzhkYfXPEGMu4RRSx8EVLBm/al3fr1F8tqfJd0Qjlht5TBN+JDawih
    qlwE4bhR3PhMI5x/wFWWigNo2oM4VOBf/KDcE7Pw4lZIX/8vWNs+KlijzIGri1LyV7C6EVORhCaH
    SEnCCtZz5DDq/gnxULwCbzrVKrl2r2/K9coVlGp12+LoStMeo747KaLgiXAyAR+4si13SpknEWV3
    BVdSsFCHiW4/SxGAVHEGb7suBKoTKSX4XAal+jAGxLuQiTmpwCByfIrqi3Nlxbm2z7RHSgxcwuBJ
    eGC3HvgUJFVmMPovIoYyAmNxbQSTMWtzbzCVlL/mYrsEg2SG8WvhPeLvMor10KFcVqsFq1yu54IS
    hnSE9EAOVMwQeUvfwav0coFP5J36aKsj3hFEWJifuifK94UkHWC3ttAuFzmYitmDm+TZzFKStztJ
    gywN99QzwCgi1bMIqaKJoS76hXl0JQDPxQ7hXaX8iQJrV1h/vOFxhnkUxNuajZfkk0YxlZ0D80eG
    JuLJh8YqnT+fFlt6jbtTIe1WR6AGfyM3m4AeJUxM+hCiq1iP1KQB7FG4RJewXL8pWPVLA4xm5nJs
    BiDdnNXrgnVZMRAfEueBe4DdYuKRZtRvxA9+N3borxBGNIwIS2nfIe5rV7Ak5Vtn4RodceC4r6s5
    eEnP1phY1FT8SYk53zOHE7NgPcJ4s3V38axyAMu0o6dkWXk/lhk7dDyWaZPHYVlyN8OJ740ozE7O
    td7daE+KMSZmmDg5FG3P66NPn22yndgeMFBwMBxHKZIJqxX5/Csa9nQgu2NldvVVq0fDgJEYvC4w
    Fu6hJbF9oIqVm9E3wU2c107fA0FkeA90MlWL+yJ6QBwGaVIonxirWyIKijwgU02ZnfQrXri0mtPR
    fMm9LTfa9LqyuMrqfk2iODSqrvAd8b+H1Q4CKd4IO2ZkP0W9sHVwv1EV74xs341nvvBwWhrIYmNn
    bYbbhTrp04vhhFlAf1BPTb/SoHjfF4x1iAyNR0T2jHZTK1i1q702dCl3W+JkS5YLnmSLE3P5sn66
    ujq3qBgRRj2iwFvgP452gvW41yj9eseaXn2z38Y+U5+sDDCjrwRylsw6WNNw+WkwaQHDB+MUkCvb
    RxPfOYsNv+FIy0bSdjKSTzvIbm3cZ79S3XPN9/VBfuoKzw1ZA0VUlI/VZZ6jTlW6Wu2IpTtvYy42
    lVHpbinLz+LWgn0YMq3ZJ/KrqW2HIfgYKIRzZdlJ3PJDV0hGnSMkq1H80LpuRZfnHDaOToF8iwkt
    z2NIkhneFg6xVaqMr8fVcbnsXZdIheTb+ttnZ4mpKyQc39DHW035RnH5l5LWfyhei244GQAA#>
    #endregion
    
    <#
        .NOTES
        --------------------------------------------------------------------------------
         Code generated by:  SAPIEN Technologies, Inc., PowerShell Studio 2016 v5.2.120
         Generated on:       5/6/2016 4:56 AM
         Generated by:       Darius
        --------------------------------------------------------------------------------
        .DESCRIPTION
            GUI script generated by PowerShell Studio 2016
    #>
    #----------------------------------------------
    #region Application Functions
    #----------------------------------------------
    
    #endregion Application Functions
    
    #----------------------------------------------
    # Generated Form Function
    #----------------------------------------------
    function Call-Test3_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()
    	$formGroupManagement = New-Object 'System.Windows.Forms.Form'
    	$buttonCancel = New-Object 'System.Windows.Forms.Button'
    	$buttonSubmit = New-Object 'System.Windows.Forms.Button'
    	$DGV = New-Object 'System.Windows.Forms.DataGridView'
    	$Group = New-Object 'System.Windows.Forms.DataGridViewTextBoxColumn'
    	$Approval = New-Object 'System.Windows.Forms.DataGridViewComboBoxColumn'
    	$Authority = New-Object 'System.Windows.Forms.DataGridViewTextBoxColumn'
    	$InitialFormWindowState = New-Object 'System.Windows.Forms.FormWindowState'
    	#endregion Generated Form Objects
    
    	#----------------------------------------------
    	# User Generated Script
    	#----------------------------------------------
    	
    	$formGroupManagement_Load={
    		#TODO: Initialize Form Controls here
    		
    		if ($1 = 1) { $DGV.Rows.Add('Test1') }
    		
    		if ($2 = 2) { $DGV.Rows.Add('Test2') }
    		
    		if ($3 = 3) { $DGV.Rows.Add('Test3') }
    		
    		$formGroupManagement.width = $DGV.width
            $DGV.Width = ($DGV.Width + 46)
    		foreach ($row in $DGV.Rows) { ($totalheight += $row.height) }
    		$DGV.Height = $totalheight + $DGV.ColumnHeadersHeight
    		$DGV.Height = ($DGV.ColumnHeadersHeight + ($DGV.Rows.GetRowsHeight('Displayed')) + 2)
    		
    	}
    	
    	#region Control Helper Functions
    	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 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.
    	
    		#>
    		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()
    	}
    	
    	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($Table -eq $null)
    		{
    			$Table = New-Object System.Data.DataTable
    		}
    	
    		if($InputObject-is [System.Data.DataTable])
    		{
    			$Table = $InputObject
    		}
    		else
    		{
    			if(-not $RetainColumns -or $Table.Columns.Count -eq 0)
    			{
    				#Clear out the Table Contents
    				$Table.Clear()
    	
    				if($InputObject -eq $null){ return } #Empty Data
    				
    				$object = $null
    				#find the first non null value
    				foreach($item in $InputObject)
    				{
    					if($item -ne $null)
    					{
    						$object = $item
    						break	
    					}
    				}
    	
    				if($object -eq $null) { 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($prop.Value -ne $null)
    						{
    							try{ $type = $prop.Value.GetType() } catch {}
    						}
    	
    						if($type -ne $null) # -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
    	
    	$buttonCancel_Click={
    		#TODO: Place custom script here
    		$formGroupManagement.Close()
    	}
    	
    	$DGV_CellValidated=[System.Windows.Forms.DataGridViewCellEventHandler]{
    	#Event Argument: $_ = [System.Windows.Forms.DataGridViewCellEventArgs]
    		#TODO: Place custom script here
    		$formGroupManagement.AutoSize = $true
    		$formGroupManagement.AutoSizeMode = 'GrowAndShrink'
    		$DGV.AutoSize = $true
    		$DGV.AutoSizeRowsMode = 'AllCells'
    		$Authority.AutoSizeMode = 'AllCells'
    		#$Authority.AutoSizeMode = 'Fill'
    		$formGroupManagement.width = $DGV.width
            $DGV.Width = ($DGV.Width - 15)
            foreach($row in $DGV.Rows){($totalHeight += $row.height)}
            $DGV.Height = $totalHeight + $DGV.ColumnHeadersHeight
            $DGV.Height = ($DGV.ColumnHeadersHeight + ($DGV.Rows.GetRowsHeight('Displayed')) + 2)
            $formGroupManagement.AutoSize = $False
            $DGV.Dock = 'Fill'
            #$formGroupManagement.AutoSize = $true
    	}
    	
    	$DGV_CellContentClick=[System.Windows.Forms.DataGridViewCellEventHandler]{
    	#Event Argument: $_ = [System.Windows.Forms.DataGridViewCellEventArgs]
    		#TODO: Place custom script here
    		$formGroupManagement.AutoSize = $true
    		$formGroupManagement.AutoSizeMode = 'GrowAndShrink'
    		$DGV.AutoSize = $true
    		$DGV.AutoSizeRowsMode = 'AllCells'
    		$Authority.AutoSizeMode = 'AllCells'
    		#$Authority.AutoSizeMode = 'Fill'
            $formGroupManagement.width = $DGV.width
            $DGV.Width = ($DGV.Width - 15)
            foreach($row in $DGV.Rows){($totalHeight += $row.height)}
            $DGV.Height = $totalHeight + $DGV.ColumnHeadersHeight
            $DGV.Height = ($DGV.ColumnHeadersHeight + ($DGV.Rows.GetRowsHeight('Displayed')) + 2)
    	}
    	$buttonSubmit_Click={
    		#TODO: Place custom script here
            read-host $DGV.Rows[$_.RowIndex].Cells[$_.ColumnIndex].Value.ToString()
    	}
    	
    	$DGV_Resize={
    		#TODO: Place custom script here
    		$formGroupManagement.width = $DGV.width
    		foreach ($row in $DGV.Rows) { ($totalHeight += $row.height)}
            $DGV.Height = $totalHeight + $DGV.ColumnHeadersHeight
            $DGV.Height = ($DGV.ColumnHeadersHeight + ($DGV.Rows.GetRowsHeight('Displayed')) + 2)
    		
    	}
    	
    	# --End User Generated Script--
    	#----------------------------------------------
    	#region Generated Events
    	#----------------------------------------------
    	
    	$Form_StateCorrection_Load=
    	{
    		#Correct the initial state of the form to prevent the .Net maximized form issue
    		$formGroupManagement.WindowState = $InitialFormWindowState
    	}
    	
    	$Form_Cleanup_FormClosed=
    	{
    		#Remove all event handlers from the controls
    		try
    		{
    			$buttonCancel.remove_Click($buttonCancel_Click)
    			$buttonSubmit.remove_Click($buttonSubmit_Click)
    			$DGV.remove_CellContentClick($DGV_CellContentClick)
    			$DGV.remove_CellValidated($DGV_CellValidated)
    			$DGV.remove_Resize($DGV_Resize)
    			$formGroupManagement.remove_Load($formGroupManagement_Load)
    			$formGroupManagement.remove_Load($Form_StateCorrection_Load)
    			$formGroupManagement.remove_FormClosed($Form_Cleanup_FormClosed)
    		}
    		catch [Exception]
    		{ }
    	}
    	#endregion Generated Events
    
    	#----------------------------------------------
    	#region Generated Form Code
    	#----------------------------------------------
    	$formGroupManagement.SuspendLayout()
    	#
    	# formGroupManagement
    	#
    	$formGroupManagement.Controls.Add($buttonCancel)
    	$formGroupManagement.Controls.Add($buttonSubmit)
    	$formGroupManagement.Controls.Add($DGV)
    	$formGroupManagement.AcceptButton = $buttonSubmit
    	$formGroupManagement.AutoScaleMode = 'Inherit'
    	$formGroupManagement.AutoSize = $true
    	$formGroupManagement.AutoSizeMode = 'GrowAndShrink'
    	$formGroupManagement.CancelButton = $buttonCancel
    	$formGroupManagement.ClientSize = '277, 119'
    	$formGroupManagement.FormBorderStyle = 'Fixed3D' #makes form non-resizable via mouse controls
    	$formGroupManagement.MaximizeBox = $False
    	$formGroupManagement.MinimizeBox = $False
    	$formGroupManagement.Name = 'formGroupManagement'
    	$formGroupManagement.ShowIcon = $False
    	$formGroupManagement.SizeGripStyle = 'Hide'
    	$formGroupManagement.StartPosition = 'CenterScreen'
    	$formGroupManagement.Text = 'Group Management'
    	$formGroupManagement.add_Load($formGroupManagement_Load)
    	#
    	# buttonCancel
    	#
    	$buttonCancel.Anchor = 'Left'
    	$buttonCancel.DialogResult = 'Cancel'
    	$buttonCancel.Location = '196, 92'
    	$buttonCancel.Name = 'buttonCancel'
    	$buttonCancel.Size = '75, 23'
    	$buttonCancel.TabIndex = 2
    	$buttonCancel.Text = '&Cancel'
    	$buttonCancel.UseVisualStyleBackColor = $true
    	$buttonCancel.add_Click($buttonCancel_Click)
    	#
    	# buttonSubmit
    	#
    	$buttonSubmit.Anchor = 'Top'
    	$buttonSubmit.Location = '3, 92'
    	$buttonSubmit.Name = 'buttonSubmit'
    	$buttonSubmit.Size = '75, 23'
    	$buttonSubmit.TabIndex = 1
    	$buttonSubmit.Text = '&Submit'
    	$buttonSubmit.UseVisualStyleBackColor = $true
    	$buttonSubmit.add_Click($buttonSubmit_Click)
    	#
    	# DGV
    	#
    	$DGV.AllowUserToAddRows = $False
    	$DGV.AllowUserToDeleteRows = $False
    	$DGV.AllowUserToResizeColumns = $False
    	$DGV.AllowUserToResizeRows = $False
    	$DGV.AutoSizeColumnsMode = 'DisplayedCells'
    	$DGV.AutoSizeRowsMode = 'AllCells'
    	$DGV.BorderStyle = 'None'
    	$DGV.ColumnHeadersHeightSizeMode = 'DisableResizing'
    	[void]$DGV.Columns.Add($Group)
    	[void]$DGV.Columns.Add($Approval)
    	[void]$DGV.Columns.Add($Authority)
    	$DGV.Location = '3, 2'
    	$DGV.Name = 'DGV'
    	$DGV.RowHeadersVisible = $False
    	$DGV.RowHeadersWidthSizeMode = 'DisableResizing'
    	$DGV.ScrollBars = 'None'
    	$DGV.Size = '268, 84'
    	$DGV.TabIndex = 0
    	$DGV.add_CellContentClick($DGV_CellContentClick)
    	$DGV.add_CellValidated($DGV_CellValidated)
    	$DGV.add_Resize($DGV_Resize)
    	#
    	# Group
    	#
    	$Group.AutoSizeMode = 'DisplayedCells'
    	$Group.HeaderText = 'Group Name'
    	$Group.Name = 'Group'
    	$Group.ReadOnly = $true
    	$Group.Resizable = 'True'
    	$Group.Width = 92
    	#
    	# Approval
    	#
    	$Approval.AutoSizeMode = 'DisplayedCells'
    	$Approval.HeaderText = 'Approval Status'
    	$Approval.Name = 'Approval'
    	$Approval.Resizable = 'True'
    	$Approval.Width = 88
        $Approval.items.add('')
        $Approval.items.add('Approved')
        $Approval.items.add('Denied')
    	#
    	# Authority
    	#
    	$Authority.AutoSizeMode = 'Fill'
    	$Authority.HeaderText = 'Approval Authority'
    	$Authority.Name = 'Authority'
    	$formGroupManagement.ResumeLayout()
    	#endregion Generated Form Code
    
    	#----------------------------------------------
    
    	#Save the initial state of the form
    	$InitialFormWindowState = $formGroupManagement.WindowState
    	#Init the OnLoad event to correct the initial state of the form
    	$formGroupManagement.add_Load($Form_StateCorrection_Load)
    	#Clean up the control events
    	$formGroupManagement.add_FormClosed($Form_Cleanup_FormClosed)
    	#Show the Form
    	return $formGroupManagement.ShowDialog()
    
    } #End Function
    
    #Call the form
    Call-Test3_psf | Out-Null
    

    • Marked as answer by djohnson627 Friday, May 6, 2016 8:57 PM
    Friday, May 6, 2016 8:57 PM
  • Finalized script.  It doesn't run correctly in PS Studio 2016 but runs fine in PS.
    #------------------------------------------------------------------------
    # Source File Information (DO NOT MODIFY)
    # Source ID: 49b20ef8-692d-44a7-bba2-bc5083e808ad
    # Source File: Test5.psf
    #------------------------------------------------------------------------
    #region File Recovery Data (DO NOT MODIFY)
    <#RecoveryData:
    /xwAAB+LCAAAAAAABADNWVlv6jgUfh9p/kOUh3lCZStt0QASy3TRtPdWwOXet8okB/DUsSPHKWV+
    /RwnbKGBOANFVaWqhPOdzd9ZnDb64Ig3kIseUcTCPwIqeNOu2K3ff7OsxndJp5QTdksZfCMetIYQ
    qNqFH0waxQ/fxYjxP+AoSy18aNqDRaDAu/hJuSvmwcWtkF78u2ClfVWwRksHLi9K+qdgdUOmQglN
    DqGShBWs53DMqPM3LIbiFXhzfH1Nak7tqlyvXkLppm5bHF1p2hPUdydF6D8RTqbgAVe25cwocyWi
    7K7gSgoWxGGi289S+CDVYglvOw74qhMqJfhKBqX6MAHEO7AUG0cCg3DsUVRfXCkrrrTt0x4qMXAI
    gyfhgt164DOQVJnB6L+IGMoQjMVjI5iMeZu7g5mk/DUT2yUYJDOMPxbOEX+XUTyPOJRqqVqwyrVa
    JkgzpCOkC3KgFgyRt/Qd3GovE/hE3qmHtjriHUGEBdmpe6I8L0RXgN1KoV0mcjAT8wdH59nMks7b
    naT+Mg331DXAKCLVswiootpQF/3CPDoSgGdih/CuIv6EvnUorL/e8PES8yiIm5qNF/1NoxjJroDZ
    LeOZcGCf3jN6d6PI0KFG8TE9j8IhcVZrSOTqh7zso8rKmgEgrpRKvYwGSiUDwJCMH7gLSN5Uf7IT
    rqcBcswdUZifI++HU57S2xgT8x8ByKFou24fvdpXPRnoHjBQcISCPgR4Ol3BQo8fpyO3E8v+vrQd
    t/keDXxGFuB2gbEglx5tP1aCrpnDEz35m+BmzsdO3wNBbHAPdDpTm1mFQZAxgygtlE9zKEyQBoUe
    kLemPNd9Dht+rOjzSJ9wMGWmRg1zM0yXUkUdyf+PrSu8sfgCwbV9X4o3wk4d3xc5OyylmcBVbpEZ
    nxGne8J51XsOSxsShyYRxpY2Jg6MIiNp7BDLih3RgI6jJcy4XW3AP6mrZsdVO64ugrEOkUGOnpM5
    Rg8N0j3y20uP7pl6eOET3HD10WFiX3afJpefVCUjwqhLFLgbDetHu3C8h0XTfL17Jz9mj/p4yf/0
    IZ+8KuRZsNrcwaKyWx2BGvDm2NfDwmAJyrguHXdhSilWSpiYIosxQbhhR4EawDZFW7m8QmJWrow3
    yO2UGm+RNTzDismWuqF+xUQ6uiT8QTz/T2OHcPnBPhISFi0PHeK8Yv/WJ73vvBJ1ElfYdg5eUurr
    i9fD6tXBMfXwCJMvWA7Ja1FuVseJMWb1ZT03q8v5WG3s0OlYHZvMZHXyQ7673Zn2psRqa/S6KuMe
    swuON4vtNxWaTIYvbSJApmwfTXznbGH49i1aZ0i0IxnJR0uR3arvNttjTvdcW//ucv/ZJ7wyZA0U
    UWE2Vu/e2w31481ioLCtTQuWFzhCMjo+yWUhdlOvbInl/1zme8DpjnHDgljl97NIfnNzQpKft4Vt
    bnhGJE+9u2VTG8lgrS2ZHtoe+WRu20EAHkYKwUrZ8smidULyNYprrbtW4vM5h42TcyDboubleQxJ
    Mo86Rn5bpeqkNrmelMturUSqJNvWL4+dJaaukHB6Q+uPMeUbxe1/eLb+A45ExX//HAAA#>
    #endregion
    
    <#
        .NOTES
        --------------------------------------------------------------------------------
         Code generated by:  SAPIEN Technologies, Inc., PowerShell Studio 2016 v5.2.120
         Generated on:       5/7/2016 5:54 PM
         Generated by:       Darius
        --------------------------------------------------------------------------------
        .DESCRIPTION
            GUI script generated by PowerShell Studio 2016
    #>
    #----------------------------------------------
    #region Application Functions
    #----------------------------------------------
    
    #endregion Application Functions
    
    #----------------------------------------------
    # Generated Form Function
    #----------------------------------------------
    function Call-Test5_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()
    	$formGroupManagement = New-Object 'System.Windows.Forms.Form'
    	$DGVPanel = New-Object 'System.Windows.Forms.Panel'
    	$DGV = New-Object 'System.Windows.Forms.DataGridView'
    	$buttonCancel = New-Object 'System.Windows.Forms.Button'
    	$buttonSubmit = New-Object 'System.Windows.Forms.Button'
    	$Group = New-Object 'System.Windows.Forms.DataGridViewTextBoxColumn'
    	$Approval = New-Object 'System.Windows.Forms.DataGridViewComboBoxColumn'
    	$Authority = New-Object 'System.Windows.Forms.DataGridViewTextBoxColumn'
    	$InitialFormWindowState = New-Object 'System.Windows.Forms.FormWindowState'
    	#endregion Generated Form Objects
    
    	#----------------------------------------------
    	# User Generated Script
    	#----------------------------------------------
    	
    	
    	$formGroupManagement_Load={
    		#TODO: Initialize Form Controls here
    		
    		if ($1 = 1) { $DGV.Rows.Add('Test1') }
    		if ($2 = 2) { $DGV.Rows.Add('Test2') }
    		if ($3 = 3) { $DGV.Rows.Add('Test3') }
    		if ($1 = 1) { $DGV.Rows.Add('Test1') }
    		if ($2 = 2) { $DGV.Rows.Add('Test2') }
    		if ($3 = 3) { $DGV.Rows.Add('Test3') }		
    		if ($1 = 1) { $DGV.Rows.Add('Test1') }
    		if ($2 = 2) { $DGV.Rows.Add('Test2') }
    		if ($3 = 3) { $DGV.Rows.Add('Test3') }
    		if ($1 = 1) { $DGV.Rows.Add('Test1') }
            
            $formGroupManagement.AutoSize = $true
    		$formGroupManagement.AutoSizeMode = 'GrowAndShrink'
    		$DGV.AutoSize = $true
    		$DGV.AutoSizeRowsMode = 'AllCells'
    		$Authority.AutoSizeMode = 'AllCells'
            $DGVPanel.AutoSize = $True
            $DGVPanel.AutoSizeMode = 'GrowAndShrink'		
            $formGroupManagement.width = $DGVPanel.width
            foreach ($row in $DGV.Rows) { ($totalHeight += $row.height) }
    		$DGV.Height = $totalHeight + $DGV.ColumnHeadersHeight
    		$DGV.Height = ($DGV.ColumnHeadersHeight + ($DGV.Rows.GetRowsHeight('Displayed')) + 2)
    		$formGroupManagement.AutoSize = $false
    		$DGV.AutoSize = $false
    		$formGroupManagement.AutoSize = $true
    		$DGV.AutoSize = $true
    		$DGV.Dock = 'Fill'	
    	}
    	
    	#region Control Helper Functions
    	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 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.
    	
    		#>
    		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()
    	}
    	
    	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($Table -eq $null)
    		{
    			$Table = New-Object System.Data.DataTable
    		}
    	
    		if($InputObject-is [System.Data.DataTable])
    		{
    			$Table = $InputObject
    		}
    		else
    		{
    			if(-not $RetainColumns -or $Table.Columns.Count -eq 0)
    			{
    				#Clear out the Table Contents
    				$Table.Clear()
    	
    				if($InputObject -eq $null){ return } #Empty Data
    				
    				$object = $null
    				#find the first non null value
    				foreach($item in $InputObject)
    				{
    					if($item -ne $null)
    					{
    						$object = $item
    						break	
    					}
    				}
    	
    				if($object -eq $null) { 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($prop.Value -ne $null)
    						{
    							try{ $type = $prop.Value.GetType() } catch {}
    						}
    	
    						if($type -ne $null) # -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
    	
    	$buttonCancel_Click={
    		#TODO: Place custom script here
    		$formGroupManagement.Close()
    	}
    	
    	$DGV_CellValidated=[System.Windows.Forms.DataGridViewCellEventHandler]{
    	#Event Argument: $_ = [System.Windows.Forms.DataGridViewCellEventArgs]
    		#TODO: Place custom script here
    		$formGroupManagement.AutoSize = $true
    		$formGroupManagement.AutoSizeMode = 'GrowAndShrink'
    		$DGV.AutoSize = $true
    		$DGV.AutoSizeRowsMode = 'AllCells'
    		$Authority.AutoSizeMode = 'AllCells'
            $DGVPanel.AutoSize = $True		
            $DGVPanel.AutoSizeMode = 'GrowAndShrink'
            $formGroupManagement.width = $DGVPanel.width
    		foreach ($row in $DGV.Rows) { ($totalHeight += $row.height) }
    		$DGV.Height = $totalHeight + $DGV.ColumnHeadersHeight
    		$DGV.Height = ($DGV.ColumnHeadersHeight + ($DGV.Rows.GetRowsHeight('Displayed')) + 2)
    		$formGroupManagement.AutoSize = $false
    		$DGV.AutoSize = $false
            $formGroupManagement.Width = $DGVPanel.Width
    		$formGroupManagement.AutoSize = $true
    		$DGV.AutoSize = $true
    		$DGV.Dock = 'Fill'
    	}
    	
    	$DGV_CellContentClick=[System.Windows.Forms.DataGridViewCellEventHandler]{
    	#Event Argument: $_ = [System.Windows.Forms.DataGridViewCellEventArgs]
    		#TODO: Place custom script here
    		$formGroupManagement.AutoSize = $true
    		$formGroupManagement.AutoSizeMode = 'GrowAndShrink'
    		$DGV.AutoSize = $true
    		$DGV.AutoSizeRowsMode = 'AllCells'
    		$Authority.AutoSizeMode = 'AllCells'
            $DGVPanel.AutoSize = $True
            $DGVPanel.AutoSizeMode = 'GrowAndShrink'		
            $formGroupManagement.width = $DGVPanel.width
    		foreach ($row in $DGV.Rows) { ($totalHeight += $row.height) }
    		$DGV.Height = $totalHeight + $DGV.ColumnHeadersHeight
    		$DGV.Height = ($DGV.ColumnHeadersHeight + ($DGV.Rows.GetRowsHeight('Displayed')) + 2)
    		$formGroupManagement.AutoSize = $false
    		$DGV.AutoSize = $false
            $formGroupManagement.Width = $DGVPanel.Width
    		$formGroupManagement.AutoSize = $true
    		$DGV.AutoSize = $true
    		$DGV.Dock = 'Fill'
    	}
    	
    	$buttonSubmit_Click={
    		#TODO: Place custom script here
    		#Read-Host $DGV.rows[$_.rowindex].Cells[$_.columnindex].Value.ToString
            write-host "DGVPanel Size: " $DGVPanel.size, "DGV Size: " $DGV.Size, "Form Size: "$formGroupManagement.size
    	}
    	
    	$DGVPanel_Resize={
    		#TODO: Place custom script here
    		$formGroupManagement.AutoSize = $true
    		$formGroupManagement.AutoSizeMode = 'GrowAndShrink'
    		$DGVPanel.AutoSize = $true
            $DGVPanel.AutoSizeMode = 'GrowAndShrink'
    		$DGV.AutoSizeRowsMode = 'AllCells'
    		$Authority.AutoSizeMode = 'AllCells'
            $formGroupManagement.width = $DGV.width
    		foreach ($row in $DGV.Rows) { ($totalHeight += $row.height) }
    		$DGVPanel.Height = $totalHeight + $DGV.ColumnHeadersHeight
    		$DGVPanel.Height = ($DGV.ColumnHeadersHeight + ($DGV.Rows.GetRowsHeight('Displayed')))
    		$DGV.Dock = 'Fill'
    	    $formGroupManagement.Height = ($DGVPanel.Height + 95)
            $formGroupManagement.Width = $DGVPanel.Width
    	}
    	# --End User Generated Script--
    	#----------------------------------------------
    	#region Generated Events
    	#----------------------------------------------
    	
    	$Form_StateCorrection_Load=
    	{
    		#Correct the initial state of the form to prevent the .Net maximized form issue
    		$formGroupManagement.WindowState = $InitialFormWindowState
    	}
    	
    	$Form_Cleanup_FormClosed=
    	{
    		#Remove all event handlers from the controls
    		try
    		{
    			$DGV.remove_CellContentClick($DGV_CellContentClick)
    			$DGV.remove_CellValidated($DGV_CellValidated)
    			$buttonCancel.remove_Click($buttonCancel_Click)
    			$buttonSubmit.remove_Click($buttonSubmit_Click)
    			$formGroupManagement.remove_Load($formGroupManagement_Load)
    			$formGroupManagement.remove_Load($Form_StateCorrection_Load)
    			$formGroupManagement.remove_FormClosed($Form_Cleanup_FormClosed)
    		}
    		catch [Exception]
    		{ }
    	}
    	#endregion Generated Events
    
    	#----------------------------------------------
    	#region Generated Form Code
    	#----------------------------------------------
    	$formGroupManagement.SuspendLayout()
    	$DGVPanel.SuspendLayout()
    	#
    	# formGroupManagement
    	#
    	$formGroupManagement.Controls.Add($DGVPanel)
    	$formGroupManagement.Controls.Add($buttonCancel)
    	$formGroupManagement.Controls.Add($buttonSubmit)
    	$formGroupManagement.AcceptButton = $buttonSubmit
    	$formGroupManagement.AutoScaleMode = 'Inherit'
    	$formGroupManagement.AutoSize = $True
    	$formGroupManagement.AutoSizeMode = 'GrowAndShrink'
    	$formGroupManagement.CancelButton = $buttonCancel
    	$formGroupManagement.ClientSize = '303, 155'
    	$formGroupManagement.FormBorderStyle = 'Fixed3D'
    	$formGroupManagement.MaximizeBox = $False
    	$formGroupManagement.MinimizeBox = $False
    	$formGroupManagement.Name = 'formGroupManagement'
    	$formGroupManagement.ShowIcon = $False
    	$formGroupManagement.SizeGripStyle = 'Hide'
    	$formGroupManagement.StartPosition = 'CenterScreen'
    	$formGroupManagement.Text = 'Group Management'
    	$formGroupManagement.add_Load($formGroupManagement_Load)
    	#
    	# DGVPanel
    	#
        $DGVPanel.BorderStyle = 'None'
    	$DGVPanel.Controls.Add($DGV)
    	$DGVPanel.Location = '5, 13'
    	$DGVPanel.Name = 'DGVPanel'
    	$DGVPanel.Size = '291, 100'
    	$DGVPanel.TabIndex = 3
        $DGVPanel.AutoSize = $True
        $DGVPanel.AutoSizeMode = 'GrowAndShrink'
        $DGVPanel.add_Resize($DGVPanel_Resize)
    	#
    	# DGV
    	#
    	$DGV.AllowUserToAddRows = $False
    	$DGV.AllowUserToDeleteRows = $False
    	$DGV.AllowUserToResizeColumns = $False
    	$DGV.AllowUserToResizeRows = $False
    	$DGV.AutoSizeColumnsMode = 'DisplayedCells'
    	$DGV.AutoSizeRowsMode = 'AllCells'
    	$DGV.BorderStyle = 'None'
    	$DGV.ColumnHeadersHeightSizeMode = 'DisableResizing'
    	[void]$DGV.Columns.Add($Group)
    	[void]$DGV.Columns.Add($Approval)
    	[void]$DGV.Columns.Add($Authority)
    	$DGV.Dock = 'Fill'
    	$DGV.Location = '0, 0'
    	$DGV.Name = 'DGV'
    	$DGV.RowHeadersVisible = $False
    	$DGV.RowHeadersWidthSizeMode = 'DisableResizing'
    	$DGV.ScrollBars = 'None'
    	$DGV.Size = '291, 100'
    	$DGV.TabIndex = 0
    	$DGV.add_CellContentClick($DGV_CellContentClick)
    	$DGV.add_CellValidated($DGV_CellValidated)
    	#
    	# buttonCancel
    	#
    	$buttonCancel.Anchor = 'Bottom, Right'
    	$buttonCancel.DialogResult = 'Cancel'
    	$buttonCancel.Location = '220, 126'
    	$buttonCancel.Name = 'buttonCancel'
    	$buttonCancel.Size = '50, 23'
    	$buttonCancel.TabIndex = 2
    	$buttonCancel.Text = '&Cancel'
    	$buttonCancel.UseVisualStyleBackColor = $True
    	$buttonCancel.add_Click($buttonCancel_Click)
    	#
    	# buttonSubmit
    	#
    	$buttonSubmit.Anchor = 'Bottom, Left'
    	$buttonSubmit.Location = '5, 126'
    	$buttonSubmit.Name = 'buttonSubmit'
    	$buttonSubmit.Size = '50, 23'
    	$buttonSubmit.TabIndex = 1
    	$buttonSubmit.Text = '&Submit'
    	$buttonSubmit.UseVisualStyleBackColor = $True
    	$buttonSubmit.add_Click($buttonSubmit_Click)
    	#
    	# Group
    	#
    	$Group.AutoSizeMode = 'DisplayedCells'
    	$Group.HeaderText = 'Group Name'
    	$Group.Name = 'Group'
    	$Group.ReadOnly = $True
    	$Group.Resizable = 'True'
    	$Group.Width = 92
    	#
    	# Approval
    	#
    	$Approval.AutoSizeMode = 'DisplayedCells'
    	$Approval.HeaderText = 'Approval Status'
    	[void]$Approval.Items.Add('')
        [void]$Approval.Items.Add('Approved')
    	[void]$Approval.Items.Add('Denied')
    	$Approval.Name = 'Approval'
    	$Approval.Resizable = 'True'
    	$Approval.Width = 88
    	#
    	# Authority
    	#
    	$Authority.AutoSizeMode = 'Fill'
    	$Authority.HeaderText = 'Approving Authority'
    	$Authority.Name = 'Authority'
    	$DGVPanel.ResumeLayout()
    	$formGroupManagement.ResumeLayout()
    	#endregion Generated Form Code
    
    	#----------------------------------------------
    
    	#Save the initial state of the form
    	$InitialFormWindowState = $formGroupManagement.WindowState
    	#Init the OnLoad event to correct the initial state of the form
    	$formGroupManagement.add_Load($Form_StateCorrection_Load)
    	#Clean up the control events
    	$formGroupManagement.add_FormClosed($Form_Cleanup_FormClosed)
    	#Show the Form
    	return $formGroupManagement.ShowDialog()
    
    } #End Function
    
    #Call the form
    Call-Test5_psf | Out-Null
    

    Sunday, May 8, 2016 3:34 AM