DataGrid reading

This topic contains 12 replies, has 2 voices, and was last updated by Profile photo of Rawdogz Rawdogz 1 month ago.

  • Author
    Posts
  • #63246
    Profile photo of Rawdogz
    Rawdogz
    Participant

    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

  • #63256
    Profile photo of Dan Potter
    Dan Potter
    Participant

    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)"}
    	
    	
    }
    
    
    
    
  • #63358
    Profile photo of Rawdogz
    Rawdogz
    Participant

    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.

  • #63487
    Profile photo of Dan Potter
    Dan Potter
    Participant

    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)"

    }

  • #63508
    Profile photo of Rawdogz
    Rawdogz
    Participant

    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.

  • #63543
    Profile photo of Dan Potter
    Dan Potter
    Participant

    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.

    postimage

  • #63546
    Profile photo of Dan Potter
    Dan Potter
    Participant

    sample code for the job tracker is here.

  • #63673
    Profile photo of Rawdogz
    Rawdogz
    Participant

    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.

  • #63679
    Profile photo of Dan Potter
    Dan Potter
    Participant

    load-datagrideview function is spelled wrong.

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

  • #64101
    Profile photo of Rawdogz
    Rawdogz
    Participant

    Thanks for all your help Dan.

  • #65068
    Profile photo of Rawdogz
    Rawdogz
    Participant

    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

  • #65143
    Profile photo of Dan Potter
    Dan Potter
    Participant

    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'

  • #65163
    Profile photo of Rawdogz
    Rawdogz
    Participant

    thanks

You must be logged in to reply to this topic.