Powershell:在按钮单击时添加并刷新datagridview

时间:2018-02-22 10:07:26

标签: winforms powershell datagridview

我正在尝试创建一个带有GUI的工具,该GUI包含许多复选框,向上/向下和组合框形式控件,允许用户:

  • 为每个表单控件选择一个值
  • 单击按钮将所有选定值“添加”到数组或对象
  • 将此对象添加到GUI底部的datagridview
  • 在每次后续按“添加”按钮
  • 时,允许将多行添加到datagridview

我很难在datagridview中显示值,更不用说在添加新值时触发刷新了。

以下是一些示例代码:

function add_Button {

    $obj = New-Object PSObject
    Add-Member -InputObject $obj -MemberType NoteProperty -Name DeployOK $chosenDeployOK
    Add-Member -InputObject $obj -MemberType NoteProperty -Name Name $chosenName
    Add-Member -InputObject $obj -MemberType NoteProperty -Name Cpu $upDown_CPU.Value
    Add-Member -InputObject $obj -MemberType NoteProperty -Name Ram $upDown_Memory.Value
    Add-Member -InputObject $obj -MemberType NoteProperty -Name AddDiskSize $upDown_SecondDisk.Value
    Add-Member -InputObject $obj -MemberType NoteProperty -Name OS $comboBox_OS.SelectedItem
    Add-Member -InputObject $obj -MemberType NoteProperty -Name VLANName $comboBox_VLAN.SelectedItem
    $global:Data += $obj

    Write-Host "Data is:"
    Write-Host $global:Data

    $dataGridView.DataSource = $null
    $dataGridView.DataSource = $global:Data
    $dataGridView.Refresh
}

$global:Data = New-Object System.Collections.ArrayList

$button_Add = new-object System.Windows.Forms.Button
$button_Add.Location = "490,10"
$button_Add.Size = "80,20"
$button_Add.Text = "Add"
$button_Add.Add_Click({add_Button})

# Base form creation
$Form = New-Object System.Windows.Forms.Form
$Form.width = 1000
$Form.height = 600
$Form.FormBorderStyle = [System.Windows.Forms.FormBorderStyle]::Fixed3D
$Form.Text = "VM Planner"
$Form.maximumsize = New-Object System.Drawing.Size(1000,600)
$Form.startposition = "centerscreen"
$Form.KeyPreview = $True
$Form.Add_KeyDown({if ($_.KeyCode -eq "Enter") {}})
$Form.Add_KeyDown({if ($_.KeyCode -eq "Escape")
{$Form.Close()}})


$dataGridView = New-Object System.Windows.Forms.DataGridView
$dataGridView.Size= "950,150"
$dataGridView.Location = "10,400"
$form.Controls.Add($dataGridView)

行:

    Write-Host "Data is:"
    Write-Host $global:Data

仅用于调试目的,以便我可以检查$ Data实际上是否保存了我在单击“添加”按钮时创建的对象。

来自控制台的示例输出:

Data is:
@{Name=; Cpu=1; Ram=1023; AddDiskSize=0; OS=Operating System; VLANName=VLAN-99}

...以及随后点击“添加”按钮,在表单控件中选择了不同的值:

@{Name=; Cpu=4; Ram=3071; AddDiskSize=0; OS=Operating System; VLANName=VLAN55; ManualIntervention=ManualIntervention} @{Name=; Cpu=6; Ram=3071; AddDiskSize=0; OS=Operating System; VLANName=VLAN-10}

有什么想法吗?我是使用Windows Forms创建Powershell GUI的新手,但过去使用Powershell做了一些通用脚本。

谢谢

1 个答案:

答案 0 :(得分:0)

我必须完成并完成工作,所以尽管可能不是最简单,最简单或最有效的,但我决定按下每个按钮,将对象的Individual属性添加到datagridview中的相应列,我也必须事先定义。举个例子:

在datagrid definiton中:

$dataGridView = New-Object System.Windows.Forms.DataGridView
$dataGridView.Size= "950,150"
$dataGridView.Location = "10,400"
$dataGridView.AllowUserToAddRows = $false
$dataGridView.ColumnCount = 4
$dataGridView.Columns[1].Name = "Name"
$dataGridView.Columns[2].Name = "CPU"
$dataGridView.Columns[3].Name = "RAM"
$dataGridView.Columns[4].Name = "AddDiskSize"

在按钮功能中:

$dataGridView.Rows.Add(,$obj.Name,$obj.CPU,$obj.Ram,$obj.AddDiskSize)

然后每次按下该按钮,表单控件中当前选择的值每按一次按钮就会添加一行。

可能不是最干净但我会在这里留下这个答案,以防有人处于类似的情况并且需要尽早开始工作。