简体   繁体   English

Powershell Datagridview:避免根据单元格中的值选择行

[英]Powershell Datagridview: avoid selection of rows based on a value in cell

I have a script that shows cells with various values.我有一个脚本,显示具有各种值的单元格。 If a cell has the value '0' then it should not be possible to select the entire row.如果一个单元格的值为“0”,那么应该不可能选择整行。

例子

Here, the rows with the numbers 10, 11 and 13 should not be able to select.此处,编号为 10、11 和 13 的行应该无法选择。

I have the following script:我有以下脚本:

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

What can I do to make it possible that a user cannot select the rows with the numbers 10, 11 and 13?我该怎么做才能使用户无法选择数字为 10、11 和 13 的行?

Feedback is appreciated a lot.非常感谢您的反馈。

With kind regards, The Sting Pilot亲切的问候, The Sting Pilot

You were almost there, but you need to handle the grid's SelectionChanged event.您快到了,但您需要处理网格的SelectionChanged事件。

Add this event handler:添加此事件处理程序:

$dgvTest.Add_SelectionChanged({
  ForEach ($Row in $dgvTest.SelectedRows){
      $Row.Selected = ($Row.Cells['Number'].Value -ne 0)
  }
})

EDIT: Changing the Selected property sets the BackColor on each cell so it's no longer the default for the row.编辑:更改 Selected 属性会在每个单元格上设置 BackColor,因此它不再是该行的默认值。 You can manually set it back to your default with this code:您可以使用以下代码手动将其设置回默认值:

$dgvTest.Add_SelectionChanged({
  ForEach ($Row in $dgvTest.SelectedRows){
    if($Row.Cells['Number'].Value -eq 0){
      $Row.Selected = $false
      $Row.Cells[0..1]|%{$_.Style.BackColor = [Drawing.Color]::FromArgb(255, 224, 224, 224) }
    }
  }
})

I figured out the properties to set by putting a break point [F9] on the $Row.Selected = $false line and looking through the $Row object's properties at run time.我通过在$Row.Selected = $false行上放置一个断点 [F9] 并在运行时查看$Row对象的属性来确定要设置的属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM