1

This is the function to check if checkbox is true winforms DataGridView:

function Install {
    param (
        # OptionalParameters
    )
    
    for($i=0;$i -lt $getAppsDataGrid.RowCount;$i++){ 

        if($getAppsDataGrid.Rows[$i].Cells[3].Value -eq $true)
        {
            $i
            $getAppsDataGrid.Rows[$i].Cells[$i].Value
            write-host "cell #$i is checked"
          

          #uncheck it
          #$datagridview1.Rows[$i].Cells['exp'].Value=$false
        }
        else    
        {
          #check it
          #$datagridview1.Rows[$i].Cells['exp'].Value=$true
          write-host  "cell #$i is not-checked"

        }
    }
}

This is working so far. But I want the cell value from the current row. $getAppsDataGrid.Rows[$i].Cells[1].Value is not working in this function. But outside of this function it works. Also other things dont show in here like current var $i. Everything except "cell $i is checked / not checked is" irgnored

Output:

cell #0 is checked
cell #1 is checked
cell #2 is checked
cell #3 is not-checked
cell #4 is not-checked
cell #5 is not-checked
cell #6 is not-checked
cell #7 is not-checked
cell #8 is not-checked
cell #9 is not-checked
cell #10 is not-checked
cell #11 is not-checked

2 Answers 2

2

There is a difference between writing something to host and writing it in output (or returning it from function).

When you use Write-output $i or return $i or $i you are adding $i to the output stream, or result of the function. When calling the function if you capture the result in a variable, then the outputs will not print.

Look at this example:

function GetEvens {  
    for($i=0;$i -lt 10;$i++){ 

        if($i%2 -eq 0)
        {
            $i
            write-host "#$i is even"
        }
        else    
        {
          write-host "#$i is odd"
        }
    }
}

$evens = GetEvens

It captures the output (return values) which are even numbers in $evens but write the string "#i is odd" or "#i is even" in host.

Sign up to request clarification or add additional context in comments.

Comments

1

Inside your function, you are simply dumping $i and $getAppsDataGrid.Rows[$i].Cells[1].Value through down the pipeline, showing nothing in the console.

To write to console, use the Write-* cmdlets. This is why strings like cell #0 is checked DO show up in the console.

If you want your function to write out everything to the console, change it to something like

function Install {
    param (
        # OptionalParameters
    )

    for($i = 0; $i -lt $getAppsDataGrid.RowCount; $i++){ 
        Write-Host "Checking row $i"
        if($getAppsDataGrid.Rows[$i].Cells[3].Value) {
            Write-Host "Value ($i,$i): {0}" -f $getAppsDataGrid.Rows[$i].Cells[$i].Value
            Write-Host "cell #$i is checked"

            #uncheck it
            #$datagridview1.Rows[$i].Cells['exp'].Value=$false
        }
        else {
          #check it
          #$datagridview1.Rows[$i].Cells['exp'].Value=$true
          Write-Host  "cell #$i is not-checked"
        }
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.