DataGridview MultiSelect

Ask questions about creating Graphical User Interfaces (GUI) in PowerShell and using WinForms controls.
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 7 years and 11 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
sekou2331
Posts: 318
Last visit: Sat Oct 28, 2023 7:46 am

DataGridview MultiSelect

Post by sekou2331 »

Hi,

I am trying to take the data out of a grid to use the information for other parts of my project. But i keep getting an erorr. I set my winform to mutilselect and SelectionMode to FullRowSelect. Do i have to call it in the script? Please see my script below and the output which is really werid. I selected three different rows and it gives me the same data but also an error.
  1. $installDatagridview2.SelectedRows | ForEach-Object{
  2.             $results += [pscustomobject]@{
  3.             Username = $installDatagridview2.Rows[$_.Index].Cells[0].Value
  4.             Computername = $installDatagridview2.Rows[$_.Index].Cells[1].Value
  5.             City = $installDatagridview2.Rows[$_.Index].Cells[2].Value
  6.             IPAddress = $installDatagridview2.Rows[$_.Index].Cells[3].Value
  7.            
  8.         }
  9.         $a = $results | Select-Object Username, Computername, IPAddress
  10.        
  11.         Write-Host $a
  12.     }



@{Username=user1; Computername=Comp1; IPAddress=10.0.0.0}
ERROR: Method invocation failed because [System.Management.Automation.PSObject] does not contain a method named 'op_Addition'.
TDInstaller2.01.psf (327, 4): ERROR: At Line: 327 char: 4
ERROR: +                 $results += [pscustomobject]@{
ERROR: +                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ERROR:     + CategoryInfo          : InvalidOperation: (op_Addition:String) [], RuntimeException
ERROR:     + FullyQualifiedErrorId : MethodNotFound
ERROR:
@{Username=user1; Computername=Comp1; IPAddress=10.0.0.0}
ERROR: Method invocation failed because [System.Management.Automation.PSObject] does not contain a method named 'op_Addition'.
TDInstaller2.01.psf (327, 4): ERROR: At Line: 327 char: 4
ERROR: +                 $results += [pscustomobject]@{
ERROR: +                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ERROR:     + CategoryInfo          : InvalidOperation: (op_Addition:String) [], RuntimeException
ERROR:     + FullyQualifiedErrorId : MethodNotFound
ERROR:
@{Username=user1; Computername=Comp1; IPAddress=10.0.0.0}
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: DataGridview MultiSelect

Post by jvierra »

You have to extract the underlying data:

$data=$installDatagridview2.SelectedRows | select -expand DataBoundItem
Write-Host $data
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: DataGridview MultiSelect

Post by jvierra »

Here is a working example using a file list:
Attachments
Demo-DGVExport.psf
(13.83 KiB) Downloaded 145 times
User avatar
sekou2331
Posts: 318
Last visit: Sat Oct 28, 2023 7:46 am

Re: DataGridview MultiSelect

Post by sekou2331 »

This seems to be not outputting anything. It is just leaving white space.
User avatar
sekou2331
Posts: 318
Last visit: Sat Oct 28, 2023 7:46 am

Re: DataGridview MultiSelect

Post by sekou2331 »

Figured this out by using the code below to get the data from the datagridview.
  1. $installDatagridview2.SelectedRows | ForEach-Object{
  2.            
  3.             $Username = $_.Cells[0].Value
  4.             $Computername = $_.Cells[1].Value
  5.             $City = $_.Cells[2].Value
  6.             $IPAddress = $_.Cells[3].Value
  7.                
  8.         Write-Host "$Computername $Username $IPAddress "
This topic is 7 years and 11 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked