我有一個腳本,顯示具有各種值的單元格。如果一個單元格的值為“0”,那么應該不可能選擇整行。

此處,編號為 10、11 和 13 的行應該無法選擇。
我有以下腳本:
Function GenerateDataGrid
{
For($Index = 1; $Index -le 30; $Index )
{
$Record = [ordered]@{
Follow = $Index
Number = Random(0..5)
}
$Global:Table = New-Object 'PSObject' -Property $Record
}
$dgvTest.DataSource = [System.Collections.ArrayList]$Global:Table
$dgvTest.SelectAll()
$Rows = $dgvTest.SelectedRows
ForEach ($Row in $Rows)
{
if($dgvTest.Rows[$Row.Index].Cells['Number'].Value -eq 0)
{
# $dgvTest.Rows[$Row.Index].Readonly = $True -> Does not work
# $dgvTest.Rows[$Row.Index].Locked = $True -> Throws up an error
# $dgvTest.Rows[$Row.Index].Enabled = $False -> Throws up an error
$dgvTest.Rows[$Row.Index].DefaultCellStyle.BackColor = [System.Drawing.Color]::FromArgb(255, 224, 224, 224)
}
}
$dgvTest.ClearSelection()
}
Function ShowResults
{
$Rows = $dgvTest.SelectedRows
Clear-Host
ForEach ($Row in $Rows)
{
$Follow = $dgvTest.Rows[$Row.Index].Cells['Follow'].Value
$Number = $dgvTest.Rows[$Row.Index].Cells['Number'].Value
Write-Host "$Follow $Number"
}
}
$Global:Table = @()
Add-Type -AssemblyName System.Web
Add-Type -AssemblyName System.Windows.Forms
$frmDataGridTest = New-Object 'System.Windows.Forms.Form'
$btnShow = New-Object 'System.Windows.Forms.Button'
$chkAllItems = New-Object 'System.Windows.Forms.CheckBox'
$btnCancel = New-Object 'System.Windows.Forms.Button'
$dgvTest = New-Object 'System.Windows.Forms.DataGridView'
#
# frmDataGridTest
#
$frmDataGridTest.Controls.Add($btnShow)
$frmDataGridTest.Controls.Add($chkAllItems)
$frmDataGridTest.Controls.Add($btnCancel)
$frmDataGridTest.Controls.Add($dgvTest)
$frmDataGridTest.AutoScaleDimensions = New-Object System.Drawing.SizeF(6, 13)
$frmDataGridTest.AutoScaleMode = 'Font'
$frmDataGridTest.CancelButton = $btnCancel
$frmDataGridTest.ClientSize = New-Object System.Drawing.Size(281, 464)
$frmDataGridTest.Name = 'frmDataGridTest'
$frmDataGridTest.StartPosition = 'CenterScreen'
$frmDataGridTest.Text = 'Test DatagridView'
#
# btnShow
#
$btnShow.Location = New-Object System.Drawing.Point(106, 385)
$btnShow.Name = 'btnShow'
$btnShow.Size = New-Object System.Drawing.Size(75, 50)
$btnShow.TabIndex = 3
$btnShow.Text = 'Show selected items'
$btnShow.UseVisualStyleBackColor = $True
#
# chkAllItems
#
$chkAllItems.Location = New-Object System.Drawing.Point(22, 13)
$chkAllItems.Name = 'chkAllItems'
$chkAllItems.Size = New-Object System.Drawing.Size(240, 24)
$chkAllItems.TabIndex = 2
$chkAllItems.Text = 'Select or unselect all items'
$chkAllItems.UseVisualStyleBackColor = $True
#
# btnCancel
#
$btnCancel.DialogResult = 'Cancel'
$btnCancel.Location = New-Object System.Drawing.Point(187, 385)
$btnCancel.Name = 'btnCancel'
$btnCancel.Size = New-Object System.Drawing.Size(75, 50)
$btnCancel.TabIndex = 1
$btnCancel.Text = 'Cancel'
$btnCancel.UseVisualStyleBackColor = $True
#
# dgvTest
#
$dgvTest.AllowUserToAddRows = $False
$dgvTest.AllowUserToDeleteRows = $False
$dgvTest.AllowUserToOrderColumns = $True
$dgvTest.AllowUserToResizeRows = $False
$dgvTest.AutoSizeColumnsMode = 'AllCells'
$dgvTest.AutoSizeRowsMode = 'AllCells'
$dgvTest.ClipboardCopyMode = 'Disable'
$dgvTest.ColumnHeadersHeightSizeMode = 'AutoSize'
$dgvTest.Location = New-Object System.Drawing.Point(22, 43)
$dgvTest.Name = 'dgvTest'
$dgvTest.ReadOnly = $True
$dgvTest.Size = New-Object System.Drawing.Size(240, 315)
$dgvTest.TabIndex = 0
$frmDataGridTest.Add_Shown({GenerateDataGrid})
$dgvTest.SelectionMode = 'FullRowSelect'
$chkAllItems.Add_CheckedChanged({
if($chkAllItems.Checked)
{
$dgvTest.SelectAll()
$Rows = $dgvTest.SelectedRows
ForEach ($Row in $Rows)
{
if($dgvTest.Rows[$Row.Index].Cells['Number'].Value -eq 0)
{
$dgvTest.Rows[$Row.Index].Selected = $False
}
}
}
else
{
$dgvTest.ClearSelection()
}
})
$btnShow.Add_Click({ShowResults})
[void]$frmDataGridTest.ShowDialog()
[void]$frmDataGridTest.Dispose()
我該怎么做才能使用戶無法選擇數字為 10、11 和 13 的行?
非常感謝您的反饋。
親切的問候, The Sting Pilot
uj5u.com熱心網友回復:
你快到了,但你需要處理網格的SelectionChanged事件。
添加此事件處理程式:
$dgvTest.Add_SelectionChanged({
ForEach ($Row in $dgvTest.SelectedRows){
$Row.Selected = ($Row.Cells['Number'].Value -ne 0)
}
})
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/487517.html
