首页 > 解决方案 > 在 DataGridView 中显示表格(在表单内部)

问题描述

我正在尝试在DataGridView表单中添加一个。表格和DataGridView显示没有问题,但表格似乎没有正确显示。

这是创建表的函数:

function tableCreator(){
    $script:statusTable = @()
    $txtPath = $script:targetName
    Get-Content $txtPath | % {
        $script:statusTable += New-Object -TypeName PSObject -Property @{
            Computer   = "$_";
            Connection = "NA";
            Status     = "NA"
        }
    }
    #$script:statusTableLength = $script:statusTable.Length
}

这是DataGridView对象:

$script:statusOutput = New-Object System.Windows.Forms.DataGridView  #creating the text box System.Windows.Forms.TextBox
$script:statusOutput.Location = New-Object System.Drawing.Size(20,100) #location of the text box (px) in relation to the primary window's edges (length, height)
$script:statusOutput.Size = New-Object System.Drawing.Size(600,350) #the size in px of the text box (length, height)
$script:statusOutput.ScrollBars = "Vertical" #adding scroll bars if required
$script:statusOutput.DataSource = $script:statusTable

这是加载表单并将 设置DataSource为表的函数:

function runstatusFormFunc {
    $reviewprocessForm.Close()
    $reviewprocessForm.Visible = $false

    tableCreator
    testConnection
    $script:statusOutput.DataSource = $script:statusTable
    $runstatusForm.Add_Shown({$runstatusForm.Activate()})

    [void] $runstatusForm.ShowDialog()
}

最终输出如下所示:

https://i.stack.imgur.com/zFsNI.png

标签: winformspowershelldatagridview

解决方案


好吧,我的问题是DataGridView不接受 Array 作为DataSource. 它只接受一个 ArrayList。

$Script:statusTable = New-Object System.Collections.ArrayList

代替

$Script:statusTable = @()

解决问题,DataGridView 正确输出对象表


推荐阅读