EvanM
EvanM

Reputation: 155

Import CSV and take the property to convert to list

I have a CSV property that I am trying to import and then use that list in a dropdown box. The dropdown requires a string but everything I have done to convert the list to a string has failed. Here is what I have:

    $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:

    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:

My guess is that I am missing something small. Any help is appreciated.

EDIT:

Code for the Update-ComboBox

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

Screenshot of data: I can't show everything do to some sensitive info but I am trying to get the ProjectRoleProfile Column enter image description here

Upvotes: 1

Views: 527

Answers (1)

BenH
BenH

Reputation: 10044

The error message is saying "I can't turn a string into a combobox"

So looking at the Update-ComboBox function it has two mandatory parameters and you are only giving it one.

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

Since the Combobox parameter is first it's trying to use the string there. So Update-ComboBox $role needs to become Update-ComboBox $yourcombobox $role.

Upvotes: 1

Related Questions