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.
This topic is 4 years and 6 months 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
dlaurora
Posts: 31
Last visit: Wed Oct 02, 2019 12:51 pm

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

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
Last visit: Wed Oct 02, 2019 12:51 pm

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

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
Last visit: Wed Oct 02, 2019 12:51 pm

Re: Getting Data from DataGridView Column

Post by dlaurora »

Still getting null even if I use $datagridview.Cells['PowerState'].Value
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Getting Data from DataGridView Column

Post by jvierra »

$row.Cells['PowerState'].Value
This topic is 4 years and 6 months 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