首页 > 解决方案 > PowerShell DataGridView 复选框行单元格值

问题描述

这是检查复选框是否为真 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"

        }
    }
}

到目前为止,这是有效的。但我想要当前行的单元格值。$getAppsDataGrid.Rows[$i].Cells[1].Value在此功能中不起作用。但在此功能之外它可以工作。还有其他东西在这里没有显示,比如 current var $i。除了“单元 $i 已检查/未检查”之外的所有内容都被忽略

输出:

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

标签: winformspowershelldatagridview

解决方案


将某些内容写入主机和将其写入输出(或从函数返回)之间是有区别的。

当您使用Write-output $iorreturn $i$i您将 $i 添加到输出流或函数的结果时。调用函数时,如果将结果捕获到变量中,则不会打印输出。

看这个例子:

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

它捕获 $evens 中的偶数输出(返回值),但在主机中写入字符串“#i is odd”或“#i is even”。


推荐阅读