目前,我正在进行一个项目,希望在构建虚拟机时显示状态更新。我有一个函数,它构建了一个名称列表,并对列表进行了“素数”。然后我把这个推到一个DataGridView上。
黄金时期运转得很好。但是我有第二个函数,我想用它来更新状态。当我更新状态时,它要么删除对象中的所有其他条目,要么复制它们。我也有一些时候调用这个函数,它说它找不到$VMName,尽管我在详细的输出中看到了它。
function Set-Prime($NameArray) {
$results = @()
foreach ($Name in $namearray) {
$OBJ = New-Object -TypeName PSObject
$OBJ | Add-Member -MemberType NoteProperty -Name VMName -Value "$Name"
$OBJ | Add-Member -MemberType NoteProperty -Name Status -Value "Primed"
$results += $OBJ
Load-DataGridView $datagridview1 -Item $results
$OBJ = $null
}
}
function Update-DataGridView($VMName, $Status) {
$Update = $results | where { $_.VMName -eq $VMName }
Write-Host $Update
$Update.Status = "$Status"
[array]$results += $Update
Load-DataGridView $datagridview1 -Item $results
}编辑:
Function Create-Names
{
$namearray = @()
[int]$i = $RangeS.Text
[int]$e = $RangeE.Text
$e++
$Prefix = $tPrefix.Text
do
{
$Suffix = "{0:000}" -f $i
$namearray += $Prefix + $Suffix
$i++
}
until ($i -eq $e)
return $namearray
}当我调用Update-DataGridView函数来设置状态时,它会在各个部分完成。
下面是。
function Load-DataGridView
{
<#
.SYNOPSIS
This functions helps you load items into a DataGridView.
.DESCRIPTION
Use this function to dynamically load items into the DataGridView control.
.PARAMETER DataGridView
The DataGridView control you want to add items to.
.PARAMETER Item
The object or objects you wish to load into the DataGridView's items collection.
.PARAMETER DataMember
Sets the name of the list or table in the data source for which the DataGridView is displaying data.
#>
Param (
[ValidateNotNull()]
[Parameter(Mandatory=$true)]
[System.Windows.Forms.DataGridView]$DataGridView,
[ValidateNotNull()]
[Parameter(Mandatory=$true)]
$Item,
[Parameter(Mandatory=$false)]
[string]$DataMember
)
$DataGridView.SuspendLayout()
$DataGridView.DataMember = $DataMember
if ($Item -is [System.ComponentModel.IListSource]`
-or $Item -is [System.ComponentModel.IBindingList] -or $Item -is [System.ComponentModel.IBindingListView] )
{
$DataGridView.DataSource = $Item
}
else
{
$array = New-Object System.Collections.ArrayList
if ($Item -is [System.Collections.IList])
{
$array.AddRange($Item)
}
else
{
$array.Add($Item)
}
$DataGridView.DataSource = $array
}
$DataGridView.ResumeLayout()
}发布于 2017-09-06 18:26:30
据我所知,从本质上讲,您从Update-Datagridview函数中所做的是用一个全新的datasource加载datagridview。
默认行为是重新绘制整个datagridview并从新的datasource填充数据。因此,您将丢失上一次加载中的数据。
Load-Datagridview似乎是一个自定义函数,由于我们对它的内部工作没有可见性,所以我们无法对这种行为进行评论。但我相信,在某种程度上,它会做这样的事情:$datagridview.Datasource = $results
因此,为了保存现有的数据,这就是您要做的。
Step1:在显示之前,将原始的$results表转换为适当的数据表。我相信Sapien论坛可以把你和这个功能联系起来。这将为$results提供一个Primarykey属性。
Step2:假设您有一个列,其数据将在所有更新过程中保持不变,则可以将该列作为主键。这将作为您所做的所有更新之间的参考点。在foreach循环之后执行此操作。
例如,看起来您的VMName列可以满足这个目的。设置主键:$results.primarykey = $results.Columns[0],其中Columns[0]是VMName的对应列索引(我以为它是第一列)
Step3:在您的update-datagridview函数中,使用merge()方法而不是加载新的数据源。
为了清晰起见,让我们说更新后的结果表名为$NewResults。不要忘记将其转换为适当的Datatable。
现在您所要做的就是将您的新数据源与原始数据源合并,如下所示:$results.merge($NewResults)和您的datagridview将进行新的更新。
merge()方法还具有附加标志。也许您可以从Sapien的编辑那里找到文档。通过一些尝试和错误,我发现这是对我有用的。
$results.merge($NewResults, $false, 'add')我不知道这些旗子是什么意思,但它解决了我的复制问题。
注意:为了使合并工作,$NewResults中的值的数据类型应该与$Results的数据类型匹配。我将所有值转换为[string],以保持它的简单性。
我知道,要实现无缝输出真是太糟糕了。
https://stackoverflow.com/questions/46079390
复制相似问题