question

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

Using Winforms BindingSource from PowerShell

I am trying to use a WInforms BindingSource in PowerShell but I am having no success. A statement such as:

      $Bs = New-Object System.Windows.Forms.BindingSource
      $Bs | gm | ft

results in:

     gm : You must specify an object for the Get-Member cmdlet.........

which seems odd. Nevertheless, although it is not an object I can assign values to its properties. For example,

    $Bs.AllowNew = $TRUE
    $Bs.DataSource = $Data
    $Bs.Count

works. This is the first and only WinForms class I have used that has behaved in this way. The count value is correct so if $Data contains 4 items, $Bs.Count reports 4.

If I do something like: (I have created a form)

   $Dg = New-Object System.Windows.Forms.DataGridView
   $Dg.DataSource = $Bs

nothing is displayed. The $Data variable mentioned earlier is an arraylist with a few strings in it. This example (usually written in C#) is commonly used in many web pages where people demonstrate how to use BindingSource with DataGridView and some use arraylists as well so I have assumed that this is a valid combination but perhaps it isn't.

Oddly, if I assign it directly to $Dg.DataSource I don't get the strings displayed; I get the lengths of each string instead but that's another story I suppose.

If anyone has any ideas about how to make BindingSource work I would be very grateful.

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,

You may try Get-Member -InputObject $BS or $BS.psobject.Members.

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,

Thanks for the suggestion and both your suggestions worked. I can now probe the BindingSource objects although I still can't get it to do anything useful. I'll have another go tomorrow though.. Anyway, thanks again.

Best wishes....
Colin Bruce

0 Votes 0 ·