0

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...

Link to screenshot

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()

1 Answer 1

1

I think if you specify:

$dtgVMList.DataBindings.DefaultDataSourceUpdateMode = 0 

Then you must refresh binding manually after assignment:

$dtgVMList.DataSource= $array
$dtgVMList.Refresh

Or specify another type of refreshing mode.

Sign up to request clarification or add additional context in comments.

2 Comments

Also, first you have to clear the binding and then reassign $dtgVMList.DataSource= $null $dtgVMList.DataSource= $array $dtgVMList.Refresh
THANKS GUYS!!!! combination of all solutions above fixe the issues: - specified databinding during variable definistion - $nulling the datasource in the add function - refresh at the end of add function.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.