PowerShell:按下按钮时Datagrid不显示新数据

时间:2015-04-07 20:33:42

标签: powershell datagrid

我使用PowerShell和Windows.Forms.DataGrid来处理CSV文件中的数据。下面包含示例代码以复制我的问题。该脚本将生成一个带有数据网格和按钮的基本表单。我想要的是每次按下“添加”按钮时向数据网格添加新行。

当单击添加按钮时,当前在数据网格中添加了一个新行。您可以看到VMcount以及新数据源内容,但它不会显示在数据网格中。我发现了一些关于.autogeneratecolumn的帖子,但这个选项在powershell中不可用。只是无法弄清楚我错过了什么......

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 个答案:

答案 0 :(得分:1)

我想如果你指定:

$dtgVMList.DataBindings.DefaultDataSourceUpdateMode = 0 

然后您必须在分配后手动刷新绑定:

$dtgVMList.DataSource= $array
$dtgVMList.Refresh

或指定其他类型的刷新模式。