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.
Post Reply
User avatar
dlaurora
Posts: 31
Joined: Wed Oct 26, 2016 10:43 am

Getting Data from DataGridView Column

Post by dlaurora » Fri Sep 06, 2019 11:19 am

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.

User avatar
jvierra
Posts: 13732
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Getting Data from DataGridView Column

Post by jvierra » Fri Sep 06, 2019 11:49 am

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 » Fri Sep 06, 2019 12:12 pm

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 151 times

User avatar
jvierra
Posts: 13732
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Getting Data from DataGridView Column

Post by jvierra » Fri Sep 06, 2019 12:19 pm

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 » Fri Sep 06, 2019 3:51 pm

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

User avatar
jvierra
Posts: 13732
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Getting Data from DataGridView Column

Post by jvierra » Fri Sep 06, 2019 4:00 pm

$row.Cells['PowerState'].Value

Post Reply