Page 1 of 1

Using datagrid with arraylist

Posted: Sat Feb 24, 2018 10:04 am
by derhoeppi
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?

Re: Using datagrid with arraylist

Posted: Sat Feb 24, 2018 10:13 am
by jvierra
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.

Re: Using datagrid with arraylist

Posted: Sat Feb 24, 2018 10:56 am
by jvierra
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)

Re: Using datagrid with arraylist

Posted: Sat Feb 24, 2018 11:01 am
by jvierra
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)

Re: Using datagrid with arraylist

Posted: Sat Feb 24, 2018 11:48 am
by derhoeppi
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?

Re: Using datagrid with arraylist

Posted: Sat Feb 24, 2018 11:57 am
by jvierra
Either export to a CSV or write a custom file export.

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

Re: Using datagrid with arraylist

Posted: Sat Feb 24, 2018 11:58 am
by derhoeppi
Sorry but i solved it.
  1.     $NewData = $datagridview1.Rows | select -ExpandProperty DataBoundItem | % { "$($_.Name)=$($_.Value)" }
  2.  
  3.     $NewData | Out-File -FilePath $PSScriptRoot\Preferences.txt

Re: Using datagrid with arraylist

Posted: Sat Feb 24, 2018 12:02 pm
by jvierra
$datagridview1.DataSource | Export-Csv d:\scripts\testds.csv