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.
This topic is 6 years and 1 month 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
derhoeppi
Posts: 34
Last visit: Mon Nov 28, 2022 8:48 am

Using datagrid with arraylist

Post by derhoeppi »

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

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: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Using datagrid with arraylist

Post 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.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Using datagrid with arraylist

Post 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)
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Using datagrid with arraylist

Post 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)
User avatar
derhoeppi
Posts: 34
Last visit: Mon Nov 28, 2022 8:48 am

Re: Using datagrid with arraylist

Post 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?
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Using datagrid with arraylist

Post by jvierra »

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
Last visit: Mon Nov 28, 2022 8:48 am

Re: Using datagrid with arraylist

Post by derhoeppi »

Sorry but i solved it.
  1.     $NewData = $datagridview1.Rows | select -ExpandProperty DataBoundItem | % { "$($_.Name)=$($_.Value)" }
  2.     $NewData | Out-File -FilePath $PSScriptRoot\Preferences.txt
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Using datagrid with arraylist

Post by jvierra »

$datagridview1.DataSource | Export-Csv d:\scripts\testds.csv
This topic is 6 years and 1 month 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