winformspowershellcheckboxdatagridviewdatagridviewcheckboxcell

DataGridView with checkboxcolumn only detects first checked box


I'm working on a DataGridView with a CheckBoxColum in the first column. I want to be able to query for the indexes of all the currently checked boxes. I have this testing code, but for some reason it only returns the first checkbox checked and not the collection of all boxes checked.

[void] [System.Reflection.Assembly]::LoadWithPartialName(“System.Windows.Forms”)
[void] [System.Reflection.Assembly]::LoadWithPartialName(“System.Drawing”)
$form = New-Object System.Windows.Forms.Form
$form.Size = New-Object System.Drawing.Size(300, 200)
$form.KeyPreview = $true
$form.StartPosition = 'centerscreen'
$form.Add_KeyDown({if($_.KeyCode -eq "Escape"){$form.Close()}})
$DataGrid1 = New-Object System.Windows.Forms.DataGridView
$DataGrid1.Location = New-Object System.Drawing.Size(298,29)
$DataGrid1.Dock = "Fill"
$DataGrid1.BorderStyle = 'FixedSingle'
$DataGrid1.AlternatingRowsDefaultCellStyle.BackColor = 'LightGray'
$DataGrid1.AllowUserToAddRows = $false
$DataGrid1.RowHeadersVisible = $false
$CheckBoxColumn = New-object System.Windows.Forms.DataGridViewCheckBoxColumn
$CheckBoxColumn.Width = 50
$CheckBoxColumn.ReadOnly = $false
$DataGrid1.columns.Add($CheckBoxColumn) |out-null
$dataGrid1.columncount = 3

$DataGrid1.rows.Add($($false,'b','d')) |out-null
$DataGrid1.rows.Add($($false,'b','d')) |out-null
$DataGrid1.rows.Add($($false,'b','d')) |out-null
$DataGrid1.rows.Add($($false,'b','d')) |out-null

$form.add_Keydown({
    if($_.KeyCode -eq 70){ # the 'f' key
        for($i = 0;$i -lt $DataGrid1.Rows.Count;$i++){
            if($DataGrid1.rows[$i].Cells[0].Value.ToString() -eq "true"){
                write-host $i -ForegroundColor Magenta #output checked indexes
            }  #output checkbox state (true = checked)
            write-host $DataGrid1.rows[$i].Cells[0].Value -BackgroundColor DarkYellow 
        }
    }
})
$form.Controls.Add($DataGrid1)
$form.ShowDialog()

If, for example, the second and fourth boxes are checked, it will only report that the second box is checked and that the fourth box is unchecked (look at the dark yellow output).

Can someone point me in the right direction of why this happens and how to fix it?


Solution

  • Use EditedFormattedValue instead

    $form.add_Keydown({
        clear
        if($_.KeyCode -eq 70){ # the 'f' key
            for($i = 0;$i -lt $DataGrid1.Rows.Count;$i++){
                if($DataGrid1.rows[$i].Cells[0].EditedFormattedValue.ToString() -eq "True"){
                    write-host $i -ForegroundColor Magenta #output checked indexesf
                }  #output checkbox state (true = checked)
                write-host $DataGrid1.rows[$i].Cells[0].EditedFormattedValue -BackgroundColor DarkYellow 
            }
        }
    })
    

    If you would like to read more on Value vs EdittedFormattedValue in a Datagrid https://www.codeproject.com/Tips/777492/EditedFormattedValue-v-s-Value-in-Datagridview