I am using PowerShell with Windows.Forms.DataGrid to manipulated data from CSV file. Included sample code below to replicate my issue. The script will generate a basic form with a datagrid and a button. All I want is to add new line to the datagrid everytime "add" button is pressed.
Currently a new line is been added to the datagrid when add button is clicked. You can see the VMcount go up as well as new datasource content but it will not display in the datagrid. I found some post about .autogeneratecolumn but this option is not available in powershell. Just cant figure out what i am missing...
Function btnVMAdd(){
Write-Host "Adding Line"
Write-Host "VM COUNT: $($dtgVMList.DataSource.count) "
$array = $dtgVMList.DataSource
$array | Out-Host
$dtgVMList.DataSource | Out-Host
$object = New-Object –TypeName PSObject
$object | Add-Member –MemberType NoteProperty –Name VMName –Value "new vm"
$object | Add-Member –MemberType NoteProperty –Name IPAddr –Value "new ip"
$array.Add($object)
$dtgVMList.DataSource= $array
}
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
#This creates the form and sets its size and position
$objForm = New-Object System.Windows.Forms.Form
$objForm.Text = "Simple Form"
$objForm.Size = New-Object System.Drawing.Size(600,600)
$objForm.StartPosition = "CenterScreen"
$gbxVMList = New-Object System.Windows.Forms.GroupBox
$gbxVMList.Location = New-Object System.Drawing.Point(20,20)
$gbxVMList.size = New-Object System.Drawing.Size(500,500)
$gbxVMList.text = "VM Deployment List"
$objForm.Controls.Add($gbxVMList)
$dtgVMList = New-Object System.Windows.Forms.DataGrid
$dtgVMList.Location = New-Object System.Drawing.Point(5,15)
$dtgVMList.Size = New-Object System.Drawing.Size(490,300)
$dtgVMList.DataBindings.DefaultDataSourceUpdateMode = 0
$dtgVMList.HeaderForeColor = [System.Drawing.Color]::FromArgb(255,0,0,0)
$dtgVMList.Name = "VMListData"
$dtgVMList.DataMember = ""
$dtgVMList.TabIndex = 0
$dtgVMList.Autog
$gbxVMList.Controls.Add($dtgVMList)
$btnVMAdd = New-Object System.Windows.Forms.Button
$btnVMAdd.Location = New-Object System.Drawing.Point(150,350)
$btnVMAdd.Size = New-Object System.Drawing.Size(90,20)
$btnVMAdd.Text = "Add VM"
$btnVMAdd.Add_Click({btnVMAdd})
$gbxVMList.Controls.Add($btnVMAdd)
$Global:json = @{}
$Global:json | Add-Member -MemberType NoteProperty -Name vmlist -Value ""
$array = New-Object System.Collections.ArrayList
$object = New-Object –TypeName PSObject
$object | Add-Member –MemberType NoteProperty –Name VMName –Value "VM Name"
$object | Add-Member –MemberType NoteProperty –Name IPAddr –Value "IP Addr"
$array.Add($object)
$global:json.vmlist = $array
$dtgVMList.DataSource = $array
$array = $dtgVMList.DataSource
$object = New-Object –TypeName PSObject
$object | Add-Member –MemberType NoteProperty –Name VMName –Value "VM Name2"
$object | Add-Member –MemberType NoteProperty –Name IPAddr –Value "IP Addr"
$array.Add($object)
$dtgVMList.DataSource = $array
$array = $dtgVMList.DataSource
$object = New-Object –TypeName PSObject
$object | Add-Member –MemberType NoteProperty –Name VMName –Value "VM Name3"
$object | Add-Member –MemberType NoteProperty –Name IPAddr –Value "IP Addr"
$array.Add($object)
$dtgVMList.DataSource = $array
$objForm.Add_Shown({$objForm.Activate()})
[void] $objForm.ShowDialog()