$Form = New-Object System.Windows.Forms.Form
$Form.Text = "testform"
$Form.Size = New-Object System.Drawing.Size(250,300)
$Form.StartPosition = "Centerscreen"
$button = New-Object System.Windows.Forms.Button
$button.Location = New-Object System.Drawing.Size(50,100)
$button.Size = New-Object System.Drawing.Size(140,30)
$button.Text = "Click Here"
$button.Add_Click({[void] $form1.ShowDialog()})
$Form.controls.Add($button)
$form1 = New-Object System.Windows.Forms.Form
$form1.Size = New-Object System.Drawing.Size(500,600)
$Form1.Text = "Select row by checking checkbox test"
$dataGridView = New-Object System.Windows.Forms.DataGridView
$dataGridView.Size=New-Object System.Drawing.Size(400,500)
$form1.Controls.Add($dataGridView)
$dataGridView.ColumnCount = 2
$dataGridView.ColumnHeadersVisible = $true
$dataGridView.Columns.Insert(0, (New-Object System.Windows.Forms.DataGridViewCheckBoxColumn))
$dataGridView.Columns[0].Name = "select"
$dataGridView.Columns[1].Name = "column1"
$dataGridView.Columns[2].Name = "column2"
$dataGridView.Rows.Add($null, "test", "test2")
$dataGridView.Rows.Add($null, "test3", "test4")
$dataGridView.AllowUserToAddRows = $false
$dataGridView.AllowUserToDeleteRows = $false
$dataGridView.Columns["column1"].ReadOnly = $true
$dataGridView.Columns["column2"].ReadOnly = $true
[void] $Form.ShowDialog()
This simply creates a datagridview with a column of checkboxes, what I want is when a checkbox gets checked, the corresponding row gets selected and highligthed, but I don't know how to accomplish that.
To highlight a row, you just need to set the
Selected
property to $true:To do it when a checkbox is checked, we'll need to add some code to handle the selection when a corresponding event occurs.
According to the documentation for the
DataGridView.CellClick
event (emphasis added):So, we can simply adapt the example from the documentation page for the
DataGridView.CurrentCellDirtyStateChanged
event:If you want to keep multiple rows
Selected
, loop over each row instead: