【问题标题】:PowerShell DataGridView checkbox row cell valuePowerShell DataGridView 复选框行单元格值
【发布时间】:2021-01-18 21:16:59
【问题描述】:

这是检查复选框是否为真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 不适用于此功能。但在此功能之外它可以工作。其他的东西也没有在这里显示,比如当前的 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

【问题讨论】:

    标签: winforms powershell datagridview


    【解决方案1】:

    写一些东西到宿主和写在输出中(或从函数中返回)是有区别的。

    当您使用 Write-output $ireturn $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”。

    【讨论】:

      【解决方案2】:

      在您的函数中,您只是将$i$getAppsDataGrid.Rows[$i].Cells[1].Value 转储到管道中,控制台中没有显示任何内容。

      要写入控制台,请使用 Write-* cmdlet。这就是为什么像cell #0 is checked DO 这样的字符串会出现在控制台中的原因。

      如果您希望您的函数将所有内容写入控制台,请将其更改为类似

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-06-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-03-03
        • 2015-12-05
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多