Powershell从DataGridView隐藏行时出现错误

时间:2018-07-23 12:44:27

标签: powershell datagridview rows visibility selected

我的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的一些信息,但这似乎不是解决此特定问题的方法。

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

主要思想是,当选中一个复选框时,将隐藏除选定行之外的所有内容,并始终保持选择处于活动状态,而不会在隐藏/取消隐藏过程之后丢失该选择。

有人可以帮助我解决此问题吗?

xmlfile.xml:

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

1 个答案:

答案 0 :(得分:0)

仅在将foreach设置为Visible = $ False的foreach之前,将currentcell设置为null即可。错误对我来说消失了:

$(function() {
    $('.scroll-down').click (function() {
        $('html, body').animate({scrollTop: $('section.buckets').offset().top }, 'slow');
      return false;
    });

    /* $('section.buckets').click (function() {
        $('html, body').animate({scrollTop: $('section.more').offset().top }, 'slow');
        return false;
    });  this is the second code is tried to implement doesnt work*/
}); 

<!--Here is the img tied to down arrow icon-->
<a href="#" class="scroll-down" address="true"><i class="fa fa-angle-down" aria-hidden="true"></i></a>

<!--CSS-->
.down {
position: fixed;
top: 93%;
left: 97%;
transform: translate(-50%, -50%);
width: 60px;
height: 60px;
border-radius: 50%;
font-size:48px;
border: 2px solid #00567d;
background: #04a9f5;
box-shadow: 0 0 0 5px #fff; 0 5px 25px rgba(0,0,0,.8);
overflow: hidden;
}

.down:before {
content: '';
position: absolute;
width: 50%;
height: 100%;
background: rgba(0,0,0,.1);

}

.down .fa {
position: absolute;
left: 50%;
transform: translate(-50%, -50%);
color: #fff;
transform: translate(-50%, -50%);
animation: animate 3s linear infinite;
text-shadow: 0 2px 5px rgba(0,0,0,.2);

}

@keyframes animate {
0% {
    top: -10%;
} 
40% {
    top: 60%;
} 
60% {
    top: 30%;
} 
80% {
    top: 60%;
} 
100% {
    top: 110%;
} 
}

/* Buckets section */

.buckets {
padding: 2em 1em 1em;
background: #3E454C;

}

.buckets ul {
margin: 0;
padding: 0;
}

.buckets li {
margin-bottom: 1em;
background: white;
list-style-type: none;
}

.bucket {
padding: 1.5em;
}

/* More section */

.more {
padding: 2em;
}

/*--------------------------------------------------------------
Use flex to create a three-bucket layout
--------------------------------------------------------------*/

@media screen and (min-width: 700px) {
@supports (display: flex) {

    .buckets ul {
        display: flex;
        flex-wrap: wrap;
        justify-content: space-around;
        align-items: stretch;

    }

    .buckets li {
        width: 31%;
    }

}
}

<!--HTML-->
<section class="buckets">
<ul>
  <li>
    <img src="./images/image1.png" alt="">
      <div class="bucket">
    <h3 class="bucket-title">headline1</h3>
    <p>paragraph1</p>
    </div><!-- .bucket -->                        </li>
  <li>
    <img src="./images/image2.png" alt="">
    <div class="bucket">                
        <h3 class="bucket-title">headline2</h3>
    <p>paragraph2</p>                               
    </div><!-- .bucket -->                                      
  </li>
  <li>
   <img src="./images/image3.png" alt="">
   <div class="bucket">
    <h3 class="bucket-title">headline3</h3>
            <p>paragraph3</p>                   
   </div><!-- .bucket -->           
  </li>
  </ul>                         
</section><!-- .buckets -->     

<section class="more">
   <div class="more-content">
     <h4 class="content-title">moretitle</h4>
     <button><a href="page.html">Learn More</a></button>
 </div><!-- .more-content -->
</section><!-- .more -->  

或者如果您想选择最后一行:

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