Using datagrid with arraylist

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.
Locked
User avatar
derhoeppi
Posts: 34
Joined: Sun Apr 06, 2014 11:10 pm

Using datagrid with arraylist

Post by derhoeppi » Sat Feb 24, 2018 10:04 am

Hi,
i'm trying to use a datagrid with an arraylist as datasource. My code looks like:
  1. $buttonRefresh_Click={
  2.  
  3.     #TODO: Place custom script here
  4.  
  5.  
  6.  
  7.     $Data = [System.Collections.ArrayList](Get-Content -Path $PSScriptRoot\Preferences.txt | ConvertFrom-StringData)
  8.  
  9.     $datagrid1.DataSource = $Data
  10.  
  11.     $formMain.Refresh()
  12.  
  13. }

When i push the button i will see the elements of the $Data, but no content. I see cells like IsReadOnly, IsFixedSize, IsSynchronized, Keys, Values, SyncRoot, Count. I want to see the Content of $Data.


The plain textfile contains some lines like "Name1=Value1".
If i use the following line:
  1. $Data| out-gridview
the content is shown.

Can anyone help me to find my mistake?

jvierra
Posts: 14021
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Using datagrid with arraylist

Post by jvierra » Sat Feb 24, 2018 10:13 am

Run this at a prompt and inspect the results.

Get-Content -Path $PSScriptRoot\Preferences.txt | ConvertFrom-StringData

You cannot convert a hash table to an arraylist You can only successfully convert objects to an arraylist.

Think of the topology of the data. A hash would be one line in a grid. Recommend using a text control to display name=value airs or convert each key in the hash to an object and make the collection an arraylist.

jvierra
Posts: 14021
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Using datagrid with arraylist

Post by jvierra » Sat Feb 24, 2018 10:56 am

Actually I take that back. You don't need to convert the hash. This works as expected.

Code: Select all

$stringdata = @'
Name1=Value1
Name2=Value2
Name3=Value3
Name4=Value4
Name5=Value5
Name6=Value6
Name7=Value7
'@

$form1_Load={
	$hash = $stringdata | ConvertFrom-StringData
    $datagridview1.DataSource = [System.Collections.ArrayList]$hash
}
Or ...

Code: Select all

$hash = $stringdata | ConvertFrom-StringData
$datagridview1.DataSource = [System.Collections.ArrayList]@([pscustomobject]$hash)

jvierra
Posts: 14021
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Using datagrid with arraylist

Post by jvierra » Sat Feb 24, 2018 11:01 am

If you want to pull from a file then you will need to force the read:

$datagridview1.DataSource = [collections.arraylist]( Get-Content d:\scripts\stringdata.txt -raw | ConvertFrom-StringData)

User avatar
derhoeppi
Posts: 34
Joined: Sun Apr 06, 2014 11:10 pm

Re: Using datagrid with arraylist

Post by derhoeppi » Sat Feb 24, 2018 11:48 am

Thanks your last post works as expected - perfect.

I want to ask you one more thing. Now i'm reading content from a textfile to a datagridview. My user should be able to edit the content. After them i want to xxport the content like the structure, so i can reimport them.

The following i'm tried:
  1. $datagridview1.Rows | select -ExpandProperty DataBoundItem | Out-File -FilePath $PSScriptRoot\Preferences.txt
The header would be exported and the structure is not Name1=Value1. I think an arraylist or hashtable should be easier as datasource for the datagridview?

jvierra
Posts: 14021
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Using datagrid with arraylist

Post by jvierra » Sat Feb 24, 2018 11:57 am

Either export to a CSV or write a custom file export.

The DataSource can be looped through and saved to a file.

User avatar
derhoeppi
Posts: 34
Joined: Sun Apr 06, 2014 11:10 pm

Re: Using datagrid with arraylist

Post by derhoeppi » Sat Feb 24, 2018 11:58 am

Sorry but i solved it.
  1.     $NewData = $datagridview1.Rows | select -ExpandProperty DataBoundItem | % { "$($_.Name)=$($_.Value)" }
  2.  
  3.     $NewData | Out-File -FilePath $PSScriptRoot\Preferences.txt

jvierra
Posts: 14021
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Using datagrid with arraylist

Post by jvierra » Sat Feb 24, 2018 12:02 pm

$datagridview1.DataSource | Export-Csv d:\scripts\testds.csv

Locked