Search code examples
winformspowershellcheckboxdatagridviewdatagridcolumnheader

Add Checkbox to Header Cell in Datagridview


I have a form with a datagridview on it. In the first column of this datagridview is a CheckBoxColumn. I am trying to add a checkbox to the header of the first column to act as a select all for the checkboxes below it. I can't understand the C# examples I've found online and I haven't found any PowerShell examples. Can someone point me in the right direction of how to do this?

[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(400, 400)
$form.KeyPreview = $true

$CheckBoxColumn1 = New-object System.Windows.Forms.DataGridViewCheckBoxColumn
$CheckBoxColumn1.Width = 30
$CheckBoxColumn1.ReadOnly = $false

$DataGrid1 = New-Object System.Windows.Forms.DataGridView
$DataGrid1.Dock = "Fill"
$DataGrid1.BorderStyle = 'FixedSingle'
$DataGrid1.AlternatingRowsDefaultCellStyle.BackColor = 'LightGray'
$DataGrid1.AllowUserToAddRows = $false
$DataGrid1.RowHeadersVisible = $false
$DataGrid1.BackgroundColor = "White"
$DataGrid1.Columns.Add($CheckBoxColumn1) | Out-Null
$DataGrid1.ColumnCount = 3
$DataGrid1.Columns[1].name = 'col1'
$DataGrid1.Columns[2].name = 'col2'
$DataGrid1.Rows.add($false,'one','two')
$DataGrid1.Rows.add($false,'three','four')

##Add checkbox to header row column 0

$form.Controls.Add($DataGrid1)
$form.ShowDialog()

Solution

  • I couldn't find a way to add a checkbox to the column header cell itself, so I developed a work around. I ended up adding a checkbox to the form and strategically placed it where the header cell would be. I then handled various events to make it work correctly with the datagridview.

    Notice that for the checkbox, I used the Click event instead of the CheckStateChanged event. This is because I don't want all the cell checkboxes to uncheck everytime I click a checkbox, but only the specific box I changed and the header checkbox.

    $checkbox1 = New-Object System.Windows.Forms.CheckBox
    $checkbox1.Location = New-Object System.Drawing.Size(10,5)
    $checkbox1.Size = New-Object System.Drawing.Size(13,13)
    $checkbox1.Visible = $true
    $checkbox1.add_Click({
        if($checkbox1.Checked){
            for($i = 0; $i -lt $datagrid1.RowCount;$i++){
                $datagrid1.Rows[$i].Cells[0].Value = $true
            }
        }
        else{
            for($i = 0; $i -lt $datagrid1.RowCount;$i++){
                $datagrid1.Rows[$i].Cells[0].Value = $false
            }
        }
    })
    $form.Controls.Add($checkbox1)
    
    #Handle Events:
    $DataGrid1.add_CellContentClick({
        $datagrid1.EndEdit() #otherwise the cell value won't have changed yet
        [System.Windows.Forms.DataGridViewCellEventArgs]$e = $args[1]
        if($e.columnIndex -eq 0){
            if($datagrid1.rows[$e.RowIndex].Cells[$e.ColumnIndex].value -eq $false){
                $checkbox1.CheckState = 'unchecked'
            }
        }
    })
    

    If you want your checkbox to only appear when the datagrid populates, look into the Visible property. It can be set to $true or $false like this: $datagrid1.Visible = $false