0

DataGridView에서 첫 번째 열의 CheckBoxColum으로 작업하고 있습니다. 현재 선택된 모든 상자의 색인을 조회 할 수 있기를 원합니다. 이 테스트 코드가 있지만 웬일인지 첫 번째 체크 박스 만 반환하고 체크 된 모든 체크 박스는 반환하지 않습니다. checkboxcolumn이있는 DataGridView는 첫 번째 체크 상자 만 감지합니다.

[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(300, 200) 
$form.KeyPreview = $true 
$form.StartPosition = 'centerscreen' 
$form.Add_KeyDown({if($_.KeyCode -eq "Escape"){$form.Close()}}) 
$DataGrid1 = New-Object System.Windows.Forms.DataGridView 
$DataGrid1.Location = New-Object System.Drawing.Size(298,29) 
$DataGrid1.Dock = "Fill" 
$DataGrid1.BorderStyle = 'FixedSingle' 
$DataGrid1.AlternatingRowsDefaultCellStyle.BackColor = 'LightGray' 
$DataGrid1.AllowUserToAddRows = $false 
$DataGrid1.RowHeadersVisible = $false 
$CheckBoxColumn = New-object System.Windows.Forms.DataGridViewCheckBoxColumn 
$CheckBoxColumn.Width = 50 
$CheckBoxColumn.ReadOnly = $false 
$DataGrid1.columns.Add($CheckBoxColumn) |out-null 
$dataGrid1.columncount = 3 

$DataGrid1.rows.Add($($false,'b','d')) |out-null 
$DataGrid1.rows.Add($($false,'b','d')) |out-null 
$DataGrid1.rows.Add($($false,'b','d')) |out-null 
$DataGrid1.rows.Add($($false,'b','d')) |out-null 

$form.add_Keydown({ 
    if($_.KeyCode -eq 70){ # the 'f' key 
     for($i = 0;$i -lt $DataGrid1.Rows.Count;$i++){ 
      if($DataGrid1.rows[$i].Cells[0].Value.ToString() -eq "true"){ 
       write-host $i -ForegroundColor Magenta #output checked indexes 
      } #output checkbox state (true = checked) 
      write-host $DataGrid1.rows[$i].Cells[0].Value -BackgroundColor DarkYellow 
     } 
    } 
}) 
$form.Controls.Add($DataGrid1) 
$form.ShowDialog() 

예를 들어, 두 번째와 네 번째 상자가 선택되어 있다면, 그것은 단지 두 번째 상자가 선택되어 있는지보고와 네 번째 상자를 선택 해제되었는지 (어두운 노랑 출력 봐).

왜 이런 일이 일어나고 어떻게 해결할 수 있는지 올바른 방향으로 나를 가리킬 수 있습니까?

답변

1

사용 EditedFormattedValue 대신

$form.add_Keydown({ 
    clear 
    if($_.KeyCode -eq 70){ # the 'f' key 
     for($i = 0;$i -lt $DataGrid1.Rows.Count;$i++){ 
      if($DataGrid1.rows[$i].Cells[0].EditedFormattedValue.ToString() -eq "True"){ 
       write-host $i -ForegroundColor Magenta #output checked indexesf 
      } #output checkbox state (true = checked) 
      write-host $DataGrid1.rows[$i].Cells[0].EditedFormattedValue -BackgroundColor DarkYellow 
     } 
    } 
}) 

당신이 데이터 그리드에 EdittedFormattedValue 대 값에 더 읽고 싶다면 https://www.codeproject.com/Tips/777492/EditedFormattedValue-v-s-Value-in-Datagridview