Powershell DataGridView-遍历复选框列中的行以查看是否选中 [英] Powershell DataGridView - looping through rows in checkbox column to see if checked

查看:122
本文介绍了Powershell DataGridView-遍历复选框列中的行以查看是否选中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个Powershell表单,该表单会将所有进程都放入datagridview(到目前为止可以正常使用)。然后,我在数据源的前面添加一列,并将每一行作为一个复选框。
然后,我想在表单上按下一个按钮,以启动称为Do-Grid的功能,它旨在查看并检查第一栏中的哪些复选框。

I'm trying to create a powershell form that will get all processes into a datagridview (which works OK so far). I'm then adding an additional column infront of the datasource with each row as a checkbox. I'd then like to press a button on my form which starts the Function called Do-Grid, and it is meant to go through and see which checkboxes in this first column have been checked.

这就是我遇到的问题。我无法弄清楚如何仅在此列中循环遍历每行/单元格,并查看已选中哪个复选框(或值为true)。

And this is where I get stuck. I'm unable to figure out how to loop through each row/cell only in this column and see which checkbox has been ticked (or have the value of true).

谢谢。

Function GenerateForm {
[Void][Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")

$ButtonUpdateGrid = New-Object Windows.Forms.Button
$ButtonUpdateGrid.Location = New-Object Drawing.Point 7,15
$ButtonUpdateGrid.size = New-Object Drawing.Point 90,23
$ButtonUpdateGrid.Text = "&Get Packages"
$ButtonUpdateGrid.add_click({Get-info})

$Button1 = New-Object Windows.Forms.Button
$Button1.Location = New-Object Drawing.Point 200,15
$Button1.size = New-Object Drawing.Point 90,23
$Button1.Text = "columns"
$button1.add_Click($button1_OnClick)

$ButtonStartExport = New-Object Windows.Forms.Button
$ButtonStartExport.Location = New-Object Drawing.Point 100,15
$ButtonStartExport.size = New-Object Drawing.Point 90,23
$ButtonStartExport.Text = "rows"
$ButtonStartExport.add_click({Do-Grid})

$dataGridView1 = New-Object System.Windows.Forms.DataGridView
$dataGridView1.Location = New-Object Drawing.Point 7,40
$dataGridView1.size = New-Object Drawing.Point 1000,700
$dataGridView1.MultiSelect = $false
$dataGridView1.ColumnHeadersVisible = $true
$dataGridView1.RowHeadersVisible = $false

$form = New-Object Windows.Forms.Form
$form.text = "Package Exporter v0.1"
$form.Size = New-Object Drawing.Point 1024, 768
$form.topmost = 1
$form.Icon = [system.drawing.icon]::ExtractAssociatedIcon($PSHOME + "\powershell.exe")

$form.Controls.Add($dataGridView1)
$form.controls.add($Button1)
$form.controls.add($ButtonStartExport)
$form.controls.add($ButtonUpdateGrid)
#$form.add_Load($OnLoadForm)
$form.ShowDialog()
}


 Function Get-info{
    If($datagridview1.columncount -gt 0){
        $dataGridview1.DataSource = $null
        $DataGridView1.Columns.RemoveAt(0) 
    }
    $Column1 = New-Object System.Windows.Forms.DataGridViewCheckBoxColumn
    $Column1.width = 30
    $Column1.name = "Exp"
    $DataGridView1.Columns.Add($Column1) 
    $array = New-Object System.Collections.ArrayList
    $Script:procInfo = get-process | Select-Object name, company, description,     product, id, vm, fileversion
    $array.AddRange($procInfo)
    $dataGridview1.DataSource = $array
    $form.refresh()
}          


Function Do-Grid{

#?????????????????????????????????? not really sure what to do here
for($i=0;$i -le $datagridview1.Rows.Count;$i++){ 
write-host $datagridview1.Rows[$i].value
}

}

Generate Form


推荐答案

Function Do-Grid{

    for($i=0;$i -lt $datagridview1.RowCount;$i++){ 

       if($datagridview1.Rows[$i].Cells['exp'].Value -eq $true)
       {
         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"
       }
    }
}

这篇关于Powershell DataGridView-遍历复选框列中的行以查看是否选中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆