question

ColinBruce-1692 avatar image
0 Votes"
ColinBruce-1692 asked ColinBruce-1692 commented

Problem With Winforms BindingSource and PowerShell

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-server-powershell
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

IanXue-MSFT avatar image
0 Votes"
IanXue-MSFT answered ColinBruce-1692 commented

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.

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi Ian,

That was absolutely right! It works fine with the addition of the comma. I knew that a comma would preserve an array which is returned from a function with a single value but I didn't know that it also preserved types so that is extremely useful generally.

I have written a PowerShell wrapper for ever single Winforms control except BindingSource and BindingNavigation and this is the only one that behaved like this. I suspect there is some internal reason why BindingSource works in this way but I have no idea what it is. Anyway, that is resolved now.

Thanks again for your help.

Best wishes.....
Colin Bruce

0 Votes 0 ·