Getting Data from DataGridView Column

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
dlaurora
Posts: 31
Meble kuchenne na zamówienie - na wymiar - Wrocław
Joined: Wed Oct 26, 2016 10:43 am

Getting Data from DataGridView Column

Post by dlaurora »

Hello,

I'm trying to get data from a DatagridView that I filled with VMs info, the columns are Name, ResourceGroup, and PowerState.
How can I read the entire column Powerstate?

Thanks, best regards.

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

Re: Getting Data from DataGridView Column

Post by jvierra »

What do you mean be "read the entire column"?

To get column data just reference the column by name.

$row.Cells['PowerState'].Value

User avatar
dlaurora
Posts: 31
Joined: Wed Oct 26, 2016 10:43 am

Re: Getting Data from DataGridView Column

Post by dlaurora »

Tried that but the result is empty.

Attached you will find what I need to get from the grid.

Here is the code that I'm trying to get it to work.

Code: Select all

foreach ($state in $datagridview1.Rows.Cells['PowerState'].Value) 
{
$vm = $datagridview1.Rows.Cells['Name'].Value
$res = $datagridview1.Rows.Cells['ResourceGroupName'].Value
if ($state -eq "VM dealocated") {
start-azvm -name $vm -resourcegroup $res
} else{
$textbox.text += "´n " + "$vm is already running."
}
}
Attachments
Screenshot_1.png
Screenshot_1.png (22.2 KiB) Viewed 654 times

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

Re: Getting Data from DataGridView Column

Post by jvierra »

That is not what I posted. You have to enumerate the rows. "Cells" is not a property of the "Rows" collection. It is a property of a "row" object.

foreach ($row in $datagridview1.Rows){

User avatar
dlaurora
Posts: 31
Joined: Wed Oct 26, 2016 10:43 am

Re: Getting Data from DataGridView Column

Post by dlaurora »

Still getting null even if I use $datagridview.Cells['PowerState'].Value

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

Re: Getting Data from DataGridView Column

Post by jvierra »

$row.Cells['PowerState'].Value

Locked