我有一个 CSV 属性,我正在尝试导入,然后在下拉框中使用该列表。下拉列表需要一个字符串,但我为将列表转换为字符串所做的一切都失败了。这是我所拥有的:
$roles = (Import-CSV "\\Networklocation\somefile.csv" | Select ProjectRoleProfile | Where { $_.ProjectRoleProfile -ne "" }).ProjectRoleProfile # Selecting my attribute and cleaning out the empty cells
foreach ($role in $roles)
{
$role = [string]$role
Write-Host $role
Update-ComboBox $role #Function that updates my drop down box. Functions as long as I get strings.
}
错误:
ERROR: Update-ComboBox : Cannot process argument transformation on parameter 'ComboBox'. Cannot convert the "MYData" value of type "System.String" to type
ERROR: "System.Windows.Forms.ComboBox".
MyFile.ps1 (97, 18): ERROR: At Line: 97 char: 18
ERROR: + Update-ComboBox $role
ERROR: + ~~~~~
ERROR: + CategoryInfo : InvalidData: (:) [Update-ComboBox], ParameterBindingArgumentTransformationException
ERROR: + FullyQualifiedErrorId : ParameterArgumentTransformationError,Update-ComboBox
ERROR:
我的猜测是我错过了一些小东西。任何帮助表示赞赏。
编辑:
更新组合框的代码
function Update-ComboBox
{
<#
.SYNOPSIS
This functions helps you load items into a ComboBox.
.DESCRIPTION
Use this function to dynamically load items into the ComboBox control.
.PARAMETER ComboBox
The ComboBox control you want to add items to.
.PARAMETER Items
The object or objects you wish to load into the ComboBox's Items collection.
.PARAMETER DisplayMember
Indicates the property to display for the items in this control.
.PARAMETER Append
Adds the item(s) to the ComboBox without clearing the Items collection.
.EXAMPLE
Update-ComboBox $combobox1 "Red", "White", "Blue"
.EXAMPLE
Update-ComboBox $combobox1 "Red" -Append
Update-ComboBox $combobox1 "White" -Append
Update-ComboBox $combobox1 "Blue" -Append
.EXAMPLE
Update-ComboBox $combobox1 (Get-Process) "ProcessName"
.NOTES
Additional information about the function.
#>
param
(
[Parameter(Mandatory = $true)]
[ValidateNotNull()]
[System.Windows.Forms.ComboBox]
$ComboBox,
[Parameter(Mandatory = $true)]
[ValidateNotNull()]
$Items,
[Parameter(Mandatory = $false)]
[string]
$DisplayMember,
[switch]
$Append
)
if (-not $Append)
{
$ComboBox.Items.Clear()
}
if ($Items -is [Object[]])
{
$ComboBox.Items.AddRange($Items)
}
elseif ($Items -is [System.Collections.IEnumerable])
{
$ComboBox.BeginUpdate()
foreach ($obj in $Items)
{
$ComboBox.Items.Add($obj)
}
$ComboBox.EndUpdate()
}
else
{
$ComboBox.Items.Add($Items)
}
$ComboBox.DisplayMember = $DisplayMember