首页 > 解决方案 > 在 DataGridView 中移动行时出现间歇性意外结果

问题描述

根据下面的代码,我有一个基本脚本。所以这样做的目的是在按下向上或向下按钮时能够向上和向下移动记录。这在某些时候有效,但间歇性地它不会移动记录(但选定的行仍会移动)。请尝试我的代码,并将第一条记录上下移动几次,我希望你很快就会明白我的意思。

谢谢

亚当

[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing") 
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Data")

# Main Form
$Form = New-Object System.Windows.Forms.Form    
$Form.Size = New-Object System.Drawing.Size(510,450)
$Form.Text = "Test"
$Form.TopMost = $false
$Form.MinimizeBox = $false
$Form.MaximizeBox = $false
$Form.StartPosition = "CenterScreen"
$Form.FormBorderStyle = "FixedToolWindow"

# Datasource
$dt = New-Object System.Data.DataTable
[void]$dt.Columns.Add("Name")
[void]$dt.Columns.Add("Step")
[void]$dt.Rows.Add("Jack","1")
[void]$dt.Rows.Add("Jayden","2")
[void]$dt.Rows.Add("Dylan","3")
[void]$dt.Rows.Add("Ben","4")

# Datagridview
$DataGrid = New-Object System.Windows.Forms.DataGridView
$DataGrid.Location = New-Object System.Drawing.Size(10,10)
$DataGrid.Size = New-Object System.Drawing.Size(300,200)
$DataGrid.AllowUserToAddRows = $false
$DataGrid.DataSource = $dt
$DataGrid.DataBindings.DefaultDataSourceUpdateMode = 'OnPropertyChanged'
$DataGrid.AutoResizeColumns()
$Form.Controls.Add($DataGrid)

# Up Button
$upButton = New-Object System.Windows.Forms.Button
$upButton.Location = New-Object System.Drawing.Size(320,20)
$upButton.Size = New-Object System.Drawing.Size(90,40)
$upButton.Text = “Up”
$upButton.Add_Click({
    moveRow -1
})
$Form.Controls.Add($upButton)

# Down Button
$downButton = New-Object System.Windows.Forms.Button
$downButton.Location = New-Object System.Drawing.Size(320,70)
$downButton.Size = New-Object System.Drawing.Size(90,40)
$downButton.Text = “Down”
$downButton.Add_Click({
    moveRow 1
})
$Form.Controls.Add($downButton)

# Function to move record in DataGridView
function moveRow ($direction){

    if ($DataGrid.SelectedRows[0] -ne $null){

        $currentRow = $DataGrid.SelectedRows[0].Index

        # Don't move up if you are at the top
        if (($currentRow -eq 0) -and ($direction -eq -1)){
            return
        }
        # Don't move down if you are at the bottom
        elseif (($currentRow -eq ($DataGrid.Rows.Count - 1)) -and ($direction -eq 1)){
            return
        }
        else {

            # Get Current and New Values
            $currentValue = $DataGrid.Rows[$currentRow].Cells["Step"].Value
            $newRow = $currentRow + $direction
            $newValue = $DataGrid.Rows[$newRow].Cells["Step"].Value

            # Swap Values
            $DataGrid.Rows[$currentRow].Cells["Step"].Value = $newValue
            $DataGrid.Rows[$newRow].Cells["Step"].Value = $currentValue

            # Sort and refresh DataGridView        
            $DataGrid.ClearSelection()
            $DataGrid.Rows[$newRow].Selected = $true;
            $DataGrid.Sort($DataGrid.Columns["Step"], "Ascending")
        }
    }   
}

# Show Form
$Form.Add_Shown({$Form.Activate()})
[void] $Form.ShowDialog()

标签: powershelldatagridviewdatatable

解决方案


推荐阅读