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
Datagridview and System.Data.DataTable
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.
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.
-
- Posts: 55
- Last visit: Thu Jun 24, 2021 3:05 am
Re: Datagridview and System.Data.DataTable
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.
Most controls in WInForms are data enabled and can be assigned a table or specific fields of a table.
-
- Posts: 55
- Last visit: Thu Jun 24, 2021 3:05 am
Re: Datagridview and System.Data.DataTable
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
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
-
- Posts: 55
- Last visit: Thu Jun 24, 2021 3:05 am
Re: Datagridview and System.Data.DataTable
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
$assignments_new = $assignments | Select @{Name = "Server Name";Expression = {"$($_.Name) }, @{Name = "Package ID Name";Expression = {"$($_.PackageID) }
$datagridviewAssignments.DataSource = $assignments_new
Re: Datagridview and System.Data.DataTable
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
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
-
- Posts: 55
- Last visit: Thu Jun 24, 2021 3:05 am
Re: Datagridview and System.Data.DataTable
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
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
-
- Posts: 55
- Last visit: Thu Jun 24, 2021 3:05 am
Re: Datagridview and System.Data.DataTable
Sorry, first bit should have read:
[system.collections.arraylist]$data = $assignments
PS C:\Users\ab> $data | gm
TypeName: System.Data.DataTable
[system.collections.arraylist]$data = $assignments
PS C:\Users\ab> $data | gm
TypeName: System.Data.DataTable