PowerShell与一个DataGridView隐藏行的时候给了一个错误

问题描述 投票:0回答:1

当我决定从我目前的选择,这是为了突出显示,前隐藏一切取消一排我的PowerShell代码隐藏未选中的行工作,除了一个问题很好。在这种情况下,我得到以下错误:

Exception setting "Visible": "Row associated with the currency manager's position cannot be made invisible."

如果一个新的选择,通过点击逐行使用Ctrl键并只添加新行到选择完成,没有错误。但是,一旦你从现有的选择取消之前运行的隐藏函数一行或多行,最后取消行不会隐藏和PowerShell的返回提到的错误。

这样,为所有非选定行中的可见性的代码:

foreach($row in $DataGridView1.Rows){
            if (!$DataGridView1.SelectedRows.Contains($row)){
                $row.Visible = $false
            }

经过一番研究,我发现了大约SuspendBindingResumeBindingCurrentCell = $null一些信息,但它似乎并没有被这个特定问题的解决方案。

即使使用这些方法,我仍然得到同样的错误。

其主要思想是隐藏一切,除了选定行,当复选框被选中,并保持选择活跃的时候,没有隐藏,取消隐藏处理后失去它。

谁能帮我解决这个问题?

chmlfile.chml:

<?xml version="1.0" encoding="UTF-8"?>
  <fruitList>
      <fruit>
          <variety>./banana.zip</variety>
          <name>banana - the yellow one</name>
          <image>./images/banana-image.png</image>
          <weight>0.8</weight>
      </fruit>
      <fruit>
          <variety>./orange.zip</variety>
          <name>orange - the orange one</name>
          <image>./images/orange-image.png</image>
          <weight>0.7</weight>
      </fruit>
      <fruit>
          <variety>./lemon.zip</variety>
          <name>lemon - the green one</name>
          <image>./images/lemon-image.png</image>
          <weight>0.5</weight>
      </fruit>
      <fruit>
          <variety>./apple.zip</variety>
          <name>apple - the red one</name>
          <image>./images/apple-image.png</image>
          <weight>0.9</weight>
      </fruit>
            <fruit>
          <variety>./pineaple.zip</variety>
          <name>pineaple - the pineaple one</name>
          <image>./images/pineaple-image.png</image>
          <weight>1.5</weight>
       </fruit>
  </fruitList>

PowerShell代码:

Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.Application]::EnableVisualStyles()

#region begin GUI{ 

$Form                            = New-Object system.Windows.Forms.Form
$Form.ClientSize                 = '800,800'
$Form.text                       = "Gamelist Editor"
$Form.TopMost                    = $false

$DataGridView1                   = New-Object system.Windows.Forms.DataGridView
$DataGridView1.BackColor         = "#f7f7f7"
$DataGridView1.width             = 770
$DataGridView1.height            = 570
$DataGridView1.Anchor            = 'top,bottom,left,right'
$DataGridView1.location          = New-Object System.Drawing.Point(15,168)

$CheckBox1                       = New-Object system.Windows.Forms.CheckBox
$CheckBox1.text                  = "Show only selected"
$CheckBox1.AutoSize              = $false
$CheckBox1.width                 = 157
$CheckBox1.height                = 20
$CheckBox1.Anchor                = 'bottom,left'
$CheckBox1.location              = New-Object System.Drawing.Point(15,765)
$CheckBox1.Font                  = 'Microsoft Sans Serif,10'

$Label1                          = New-Object system.Windows.Forms.Label
$Label1.text                     = "no selection"
$Label1.AutoSize                 = $true
$Label1.width                    = 25
$Label1.height                   = 10
$Label1.location                 = New-Object System.Drawing.Point(250,765)
$Label1.Font                     = 'Microsoft Sans Serif,10'
$Label1.Anchor                   = 'bottom,left'

$Form.controls.AddRange(@($DataGridView1,$CheckBox1,$Label1))

#region gui events {

$DataGridView1.Add_CellMouseDown({ buffer_SelectedRows })
$DataGridView1.Add_Sorted({ Load_Buffer })
$DataGridView1.Add_MouseClick({ countSelection })
$CheckBox1.Add_CheckedChanged({ hideSelected })
#endregion events }

#endregion GUI }

$DataGridView1.ReadOnly = $true
$DataGridView1.AllowUserToAddRows = $false
$DataGridView1.SelectionMode = 'FullRowSelect'
$DataGridView1.AutoSizeColumnsMode = 16
Function importXML(){
    $xml_input = "xmlfile.xml"
    $ds = New-Object System.Data.Dataset
    $ds.ReadXml($xml_input)
    $DataGridView1.DataSource = $ds.Tables[0]
    $DataGridView1.ClearSelection()
    $DataGridView1.CurrentCell = $null
}
importXML
Function buffer_SelectedRows(){
    $global:buffer = New-Object System.Collections.Generic.List[System.Object]
    foreach($row in $DataGridView1.SelectedRows){
    $buffer.Add($row.Cells[0].Value)
    }
}
Function Load_Buffer(){
    $DataGridView1.CurrentCell = $null
    $DataGridView1.ClearSelection()
    $rowIndex = -1
    foreach($row in $DataGridView1.Rows){
        foreach($i in $buffer){
            if($row.Cells[0].Value -eq $i)
            {
            $rowIndex = $row.Index
            $DataGridView1.Rows[$rowIndex].Selected = $true
            }
        }
    }
    $Label1.Text = ($DataGridView1.SelectedRows.Count.ToString()) + " selected"
}
Function hideSelected(){
    if ($CheckBox1.Checked){
        $DataGridView1.ColumnHeadersVisible = $false
        $DataGridView1.RowHeadersVisible = $false
        foreach ($row in $DataGridView1.Rows){
            if (!$DataGridView1.SelectedRows.Contains($row)){
                $DataGridView1.DataSource.SuspendBinding
                $row.Visible = $false
                $DataGridView1.DataSource.ResumeBinding
            }
        }
        foreach($row in $DataGridView1.SelectedRows){
            $buffer.Add($row.Cells[0].Value)
        }
    } else {
        $DataGridView1.ColumnHeadersVisible = $true
        $DataGridView1.RowHeadersVisible = $true
        foreach($row in $DataGridView1.Rows){
            $DataGridView1.CurrentCell = $null
            $row.Visible = $true
        }
        Load_Buffer
    }
}
Function countSelection(){
    $Label1.Text = ($DataGridView1.SelectedRows.Count.ToString()) + " selected"
}
[void]$Form.ShowDialog()
powershell datagridview rows visibility selected
1个回答
0
投票

就在那里设置可见= $假,设置currentcell为null应该工作的foreach。错误走了我:

$DataGridView1.CurrentCell = $null
foreach ($row in $DataGridView1.Rows){
  if (!$DataGridView1.SelectedRows.Contains($row)){
            $DataGridView1.DataSource.SuspendBinding
            $row.Visible = $false
            $DataGridView1.DataSource.ResumeBinding
   }
 }

或者,如果你要选择的最后一行:

$DataGridView1.CurrentCell = $DataGridView1.rows[$DataGridView1.RowCount-1].Cells[0]
© www.soinside.com 2019 - 2024. All rights reserved.