[英]Selecting and highlight a DataGridView row by checking a checkbox
$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. 这只是创建了一个带有一列复选框的datagridview,我想要的是当一个复选框被选中,相应的行被选中并高亮显示时,但是我不知道如何实现。
To highlight a row, you just need to set the Selected
property to $true: 要突出显示一行,您只需要将
Selected
属性设置为$ true即可:
$dataGridView.Rows[$n].Selected = $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): 根据有关
DataGridView.CellClick
事件的文档(添加了重点):
For clicks in a DataGridViewCheckBoxCell , this event occurs before the check box changes value, so if you do not want to calculate the expected value based on the current value, you will typically handle the DataGridView.CellValueChanged event instead.
对于DataGridViewCheckBoxCell中的单击,此事件在复选框更改值之前发生,因此,如果您不想基于当前值计算期望值,则通常将处理DataGridView.CellValueChanged事件。 Because that event occurs only when the user-specified value is committed, which typically occurs when focus leaves the cell, you must also handle the DataGridView.CurrentCellDirtyStateChanged event .
因为该事件仅在提交用户指定的值时发生(通常在焦点离开单元格时发生),所以您还必须处理DataGridView.CurrentCellDirtyStateChanged事件 。 In that handler, if the current cell is a check box cell, call the DataGridView.CommitEdit method and pass in the Commit value.
在该处理程序中,如果当前单元格是复选框单元格,则调用DataGridView.CommitEdit方法并传入Commit值。
So, we can simply adapt the example from the documentation page for the DataGridView.CurrentCellDirtyStateChanged
event: 因此,我们可以简单地从文档页面改编
DataGridView.CurrentCellDirtyStateChanged
事件的示例:
$dataGridView.Add_CurrentCellDirtyStateChanged({
param($Sender,$EventArgs)
if($Sender.IsCurrentCellDirty){
$Sender.CommitEdit([System.Windows.Forms.DataGridViewDataErrorContexts]::Commit)
}
})
$dataGridView.Add_CellValueChanged({
param($Sender,$EventArgs)
if($EventArgs.ColumnIndex -eq 0){
$Sender.Rows[$EventArgs.RowIndex].Selected = [bool]$Sender.Rows[$EventArgs.RowIndex].Cells[$EventArgs.ColumnIndex].Value
}
})
If you want to keep multiple rows Selected
, loop over each row instead: 如果要保持多行为
Selected
,则循环遍历每行:
$dataGridView.Add_CellValueChanged({
param($Sender,$EventArgs)
if($EventArgs.ColumnIndex -eq 0){
foreach($RowIndex in 0..($Sender.Rows.Count - 1)){
$Sender.Rows[$RowIndex].Selected = [bool]$Sender.Rows[$RowIndex].Cells[$EventArgs.ColumnIndex].Value
}
}
})
$dataGridView.MultiSelect = $true
$dataGridView.SelectionMode = [System.Windows.Forms.DataGridViewSelectionMode]::FullRowSelect
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.