I'm still searching
I have this error ""System.ArgumentException: DataGridViewComboBoxCell value is not valid."
when I run this command "$dataGridView1.Rows.Add($row.name, $row.fullname, $row.extension)"
How can I load data to DataGridViewComboBoxCell?
Thanks
Here is my sample code:
#------------------------------------------------------------------------
# Source File Information (DO NOT MODIFY)
# Source ID: e49f46f8-600b-4189-8308-2909329c646a
# Source File: C:\Users\dany\Documents\SAPIEN\PowerShell Studio\Files\Test1.psf
#------------------------------------------------------------------------
<#
.NOTES
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
.DESCRIPTION
GUI script generated by PowerShell Studio 2016
#>
#----------------------------------------------
#region Application Functions
#----------------------------------------------
#endregion Application Functions
#----------------------------------------------
# Generated Form Function
#----------------------------------------------
function Call-Test1_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'
$datagridview1 = New-Object 'System.Windows.Forms.DataGridView'
$InitialFormWindowState = New-Object 'System.Windows.Forms.FormWindowState'
#endregion Generated Form Objects
#----------------------------------------------
# User Generated Script
#----------------------------------------------
#----------------------------------------------
#region Generated Form Objects
#----------------------------------------------
[System.Windows.Forms.Application]::EnableVisualStyles()
$form1 = New-Object 'System.Windows.Forms.Form'
$datagridview1 = New-Object 'System.Windows.Forms.DataGridView'
$Col1 = New-Object 'System.Windows.Forms.DataGridViewTextBoxColumn'
$Col2 = New-Object 'System.Windows.Forms.DataGridViewTextBoxColumn'
$Col3 = New-Object 'System.Windows.Forms.DataGridViewComboBoxColumn'
$InitialFormWindowState = New-Object 'System.Windows.Forms.FormWindowState'
#endregion Generated Form Objects
#----------------------------------------------
#region Generated Form Code
#----------------------------------------------
$form1.SuspendLayout()
#
# form1
#
$form1.Controls.Add($datagridview1)
$form1.ClientSize = '622, 261'
$form1.Name = 'form1'
$form1.Text = 'Form'
$form1.add_Load($form1_Load)
#
# datagridview1
#
$datagridview1.ColumnHeadersHeightSizeMode = 'AutoSize'
[void]$datagridview1.Columns.Add($Col1)
[void]$datagridview1.Columns.Add($Col2)
[void]$datagridview1.Columns.Add($Col3)
$datagridview1.Location = '42, 32'
$datagridview1.Name = 'datagridview1'
$datagridview1.Size = '513, 197'
$datagridview1.TabIndex = 0
$datagridview1.add_CellContentClick($datagridview1_CellContentClick)
$datagridview1.EditMode= "EditOnEnter"
$files = Get-ChildItem c:\windows -File | select name, fullname, extension
$rows = @($files)
foreach ($row in $rows)
{
$dataGridView1.Rows.Add($row.name, $row.fullname, $row.extension)
}
#
# Col1
#
$Col1.HeaderText = 'Col1'
$Col1.Name = 'Col1'
$Col1.ToolTipText = 'Col1'
#
# Col2
#
$Col2.HeaderText = 'Col2'
$Col2.Name = 'Col2'
$Col2.ReadOnly = $True
#
# Col3
#
$Col3.HeaderText = 'Col3'
[void]$Col3.Items.Add('1')
[void]$Col3.Items.Add('2')
[void]$Col3.Items.Add('3')
[void]$Col3.Items.Add('4')
[void]$Col3.Items.Add('5')
$Col3.Name = 'Col3'
$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()
$form1_Load={
#TODO: Initialize Form Controls here
# $files = Get-ChildItem c:\windows -File | select name, extension, fullname
}
#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
$datagridview1_CellContentClick=[System.Windows.Forms.DataGridViewCellEventHandler]{
#Event Argument: $_ = [System.Windows.Forms.DataGridViewCellEventArgs]
#TODO: Place custom script 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
{
$datagridview1.remove_CellContentClick($datagridview1_CellContentClick)
$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.ClientSize = '622, 261'
$form1.Name = 'form1'
$form1.Text = 'Form'
$form1.add_Load($form1_Load)
#
# datagridview1
#
$datagridview1.ColumnHeadersHeightSizeMode = 'AutoSize'
$datagridview1.Location = '42, 32'
$datagridview1.Name = 'datagridview1'
$datagridview1.Size = '513, 197'
$datagridview1.TabIndex = 0
$datagridview1.add_CellContentClick($datagridview1_CellContentClick)
$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
Call-Test1_psf | Out-Null