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()
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 theCheckStateChanged
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.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