Problem With Winforms BindingSource and PowerShell

Colin Bruce 41 Reputation points
2021-04-15T03:55:49.667+00:00

I am trying to use the Winforms BindingSource control from PowerShell but I get a very strange result. If I put some data in a datatable called $Data for example and include these lines:

$Bs = New-Object System.Windows.Forms.BindingSource
$Bs.DataSource = $Data

in a script which fills the datatable and then assigns $Bs to the datasource of a control such as a listbox it works as expected.

However, if I put these lines in a function such as

FUNCTION fun
{
    param ($D)

    $B = New-Object System.Windows.Forms.BindingSource
    $B.DataSource = $D

    return $B
}

and then call the function with

$Bs = Fun -D $Data

it doesn't work. I have checked and checked that the two lines in the function are the same as the two lines in the script but there are no differences.

When I have the two lines in the main body of the script I get a list of towns which is what I would expect. When I put the same lines in the function the list box displays the correct number of rows but instead of the names of each town it displays "System.Data.DataRowView".

Is it the case that it just isn't possible in PowerShell to return a BindingSource from a function?

Best wishes....
Colin Bruce

Windows for business Windows Server User experience PowerShell
0 comments No comments
{count} votes

Accepted answer
  1. Anonymous
    2021-04-15T06:46:51.867+00:00

    Hi,

    Per my test, the return value of the function is of type Object[], not BindingSource. To keep value type you can prepend a comma to $B.

    FUNCTION fun  
    {  
        param ($D)  
        $B = New-Object System.Windows.Forms.BindingSource  
        $B.DataSource = $D  
        return ,$B  
    }  
    $Bs = fun -D $data  
    

    Best Regards,
    Ian Xue

    ============================================

    If the Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.