DataGrid reading

hello everyone,

I am using Powerstudio. Currently i have a button that loads a CSV into a datagridview, that i am able to pick/sort through a list of computers and send the one i want to another datagridview. This is the part i am stuck on, in the second Datgridview i would like to read the IP address in column 2 and shut those computer down and show the process in a Listbox next to it.

The number of IP in column 2 will change depending on who is using it. so i know i will have to use a Foreach.

the second datagrideview is setup as followed

column1 column2 column3
name ip Loc

here is my button code

   

$button2_click={

$comps=$datagridview2.selectedcolumns[2]

foreach ($comp in $comps)
{
load-listbox -items $comps -listbox $listbox1

shutdown
}

thank you for any help

Selected columns = selected cells.

$form1_Load = {
	
	$datagridview1.Columns | % { $_.SortMode = 'NotSortable' }
	$datagridview1.SelectionMode = 'FullColumnSelect'
	
	(1 .. 10) | %{ $datagridview1.Rows.Add($null, 'computer' + $_) }
	
}


$button1_Click={
	
	$comps = $datagridview1.SelectedCells | ? { $_.Value -ne $null }
	
	foreach ($comp in $comps) { Write-Host "rebooting $($comp.Value)"}
	
	
}



thanks for the reply Dan. I am new to powershell as a whole and powerstudio, So spare with me. in Datagrideview2 i don’t want to have to click on any rows or columns. once the $button1 is click i would like it to automatically look in column2 where the ip’s will be and start shutting the system down one by one.

Without selecting it would be something like this. I don’t have access to my machine right now to be totally sure.

foreach($row in $datagridview.rows){

#ex if second column is ips or computers.

write-host “rebooting $($row.cells[1].value)”

}

ok,thank Dan.

i figured out what you did in the other example and i like that way better, so i will go that route. The other problem i have, is i will be package the script when i am done so i have to push the output to one of the $richtextbox i have. so far this is what i have

$button1_Click={
	
	$comps = $datagridview1.SelectedCells | ? { $_.Value -ne $null }
	
	foreach ($comp in $comps) {
Stop-computer -computername $comp -force
$mes = "shutting down"
$richtextbox1.text = $mes + $comp.value
}

issue is it only shows the last line computer10 (from example).
i change it to read $richtextbox1.appendtext = $mes + $comp.value and it errors out.

I’m not a big fan of the richtextbox. I’d add another column to the datagridview and write the results there. Down the road you’ll probably get into doing the reboots using the job tracker framework, waiting for the server to come back up and then writing an uptime back to the cell.

I did something similar.

sample code for the job tracker is here.

https://powershell.org/forums/topic/ps-winforms-in-ps-studio-looping-through-datagridview/#post-45776

thank you for the suggesting. i do like that idea better, to add another column and post the statue there. this is what i came up with but its not displaying as should.

$buttonPing_click={
	$comps = $datagridview1.SelectedCells | ? { $_.Value -ne $null }
	foreach ($comp in $comps)
	{
		$ping = Test-Connection -ComputerName $comp -Count 1 -Quiet
		if ($ping)
		{
			$i = "online"
			$mes = "shutting down $($comp.cells[3].value)"
			load-datagrideview -item $mes $datagridview1.rows[$_.Rowindex[3]].cells
		}
		else
		{
			$mes = "shutting down $($comp.cells.value)"
			Write-Host  "$i $datagridview1.rows[$_.Rowindex[3]].cells"
		}
		
	}
	

I read up on job tracker and i will use that as a automatic feature/button once i get the display fixed.

load-datagrideview function is spelled wrong.

Edit the cell directly. foreach ($comp in $comps) { $datagridview1.Rows[$comp.rowindex].Cells[3].Value = ‘rebooting’ }

Thanks for all your help Dan.

need a little help with this one, I know I am overthinking it somewhere. I would like to add color to a cell depending on the outcome of the “if” statement.
Ex
if true (just that one cell will change color)
…green
else
…red

Here is what i have now.

if.....
else
		{
			$datagridview1.rows[$comp.Rowindex].cells[4].Value = 'Offline'
			
			$datagridview1_cellvaluechanged=[System.windows.Forms.DataGridViewCellEventHandler]
			{
			  $datagridview1.rows[$comp.RowIndex].cells[4].Style.BackColor = 'Red'
								
			}	
				
			}
            					
	}

Thank you

you don’t need the event.

$datagridview1.rows[$comp.Rowindex].cells[4].Value = ‘Offline’
$datagridview1.rows[$comp.RowIndex].cells[4].Style.BackColor = ‘Red’

or

$celltoedit = $datagridview1.rows[$comp.Rowindex].cells[4]
$celltoedit.Value = ‘Offline’
$celltoedit.style.backcolor = ‘Red’

thanks