1

我有一个 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

数据截图:我无法显示对某些敏感信息所做的一切,但我正在尝试获取 ProjectRoleProfile 列 在此处输入图像描述

4

1 回答 1

1

错误消息是“我无法将字符串转换为组合框”

所以看看这个Update-ComboBox函数,它有两个强制参数,你只给它一个。

param (
    [Parameter(Mandatory = $true)]
    [ValidateNotNull()]
    [System.Windows.Forms.ComboBox]
 -> $ComboBox,
    [Parameter(Mandatory = $true)]
    [ValidateNotNull()]
 -> $Items,

由于 Combobox 参数是第一个,它试图在那里使用字符串。所以Update-ComboBox $role需要成为Update-ComboBox $yourcombobox $role

于 2017-05-15T18:53:42.960 回答