Datagridview and System.Data.DataTable

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
ClipperXTP
Posts: 27
Meble kuchenne na zamówienie - na wymiar - Wrocław
Joined: Tue Apr 28, 2020 4:07 am

Datagridview and System.Data.DataTable

Post by ClipperXTP »

Hi
I am having problems getting my head around how to update a datagridview with the content of two columns from $assignments which is a System.Data.DataTable

$assignments gives the following output in the ISE:

Name : TEXXA2
FriendlyName :Tex2
Type : Vert
PackageID : 13435

Name : Metta2
FriendlyName :Met2
Type : Vert
PackageID : 14532

Name : Raddip
FriendlyName :Rad2
Type : Vert
PackageID : 12439

I want to update my Datagridview with Name, PackageID

I have done:

$items = $assignments|
ForEach-Object{
[PSCustomObject]@{
Name = $_.name
PackageID = $_.packageID

}
}

My Datagridview updates with the correct column headers but just has one row with System.Object in each cell.
I am unable to find any help on System.Data.DataTable s that has been of use to me, would appreciate your help

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

Re: Datagridview and System.Data.DataTable

Post by jvierra »

To add a DataTable to a DataGridView use "$datagridvie.DataSource = $assignments". The columns will be created and displayed. There is no need to create another object collection.

Most controls in WInForms are data enabled and can be assigned a table or specific fields of a table.

ClipperXTP
Posts: 27
Joined: Tue Apr 28, 2020 4:07 am

Re: Datagridview and System.Data.DataTable

Post by ClipperXTP »

Thanks very much! So far so good.
What I would like to do is:
1. Just select two columns from the datatable to show in the datagridview, Name and PackageID. Right now I get all of them. I have looked high and low for how to do this, any examples I can find are C# and I am struggling to translate and I cannot find any meaningful help online or from get-member.
2. rename the two columns I choose Name -> Server Name, PackageID -> Package ID
Thanks as always for your help

ClipperXTP
Posts: 27
Joined: Tue Apr 28, 2020 4:07 am

Re: Datagridview and System.Data.DataTable

Post by ClipperXTP »

This is what I have so far but I am struggling with manipulating the datatable $Assignments to just get the two columns I want and the amended column headers

$assignments_new = $assignments | Select @{Name = "Server Name";Expression = {"$($_.Name) }, @{Name = "Package ID Name";Expression = {"$($_.PackageID) }

$datagridviewAssignments.DataSource = $assignments_new

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

Re: Datagridview and System.Data.DataTable

Post by jvierra »

Why do you think you need to do all of that. Just return only the columns you want in the data table.

You can also use a select and convert it to an arraylist then assign that to the grid.

[system.collections.arraylist]$data = $datatable | select Name, PackageId
$datagridview.DataSource = $data

ClipperXTP
Posts: 27
Joined: Tue Apr 28, 2020 4:07 am

Re: Datagridview and System.Data.DataTable

Post by ClipperXTP »

Thanks for that.
However it is still showing as a datatable and I cannot select from it:

[system.collections.arraylist]$data = $Assignments

PS C:\Users\ab> $assignments | gm


Cannot convert value "@{PackageID=}" to type "System.Collections.ArrayList". Error: "Cannot convert the "@{packageID=}" value of type "Selected.System.Data.DataTable" to type "System.Collections.ArrayList"."
At line:1 char:2
+ [system.collections.arraylist]$data = $Assignments | select packageID
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : MetadataError: (:) [], ArgumentTransformationMetadataException
+ FullyQualifiedErrorId : RuntimeException




$assignfinal = 0 .. ($Assignments.Name.Length - 1) | Select-Object @{ n = "Server Name"; e = { $Assignments.Name[$_] } }, @{ n = "Package ID"; e = { $Assignments.PackageID[$_] } } | sort "Package ID"

$assignfinalDT = ConvertTo-DataTable -InputObject $assignfinal

Update-DataGridView -DataGridView $datagridviewAssignments -Item $assignfinalDT

ClipperXTP
Posts: 27
Joined: Tue Apr 28, 2020 4:07 am

Re: Datagridview and System.Data.DataTable

Post by ClipperXTP »

Sorry, first bit should have read:

[system.collections.arraylist]$data = $assignments

PS C:\Users\ab> $data | gm


TypeName: System.Data.DataTable

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

Re: Datagridview and System.Data.DataTable

Post by jvierra »

Looks like you do not have a DataTable to start with.

Locked