powershell - 在 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()
解决方案
推荐阅读
- python - RuntimeError:找到 dtype Double 但预期 Float
- php - 带有外部 UserProvider 和 JWT 的 Laravel 8:未经身份验证
- c# - CosmosDB 区分大小写的 ORDER BY 表达式
- c# - 如何在 cshtml 视图中正确设置所选项目?
- assembly - ARM 程序集中的立即值编码
- r - 我无法使用一个预测变量/协变量获得 R 中有序逻辑函数的置信区间?
- node.js - 如何使用 Cloud Functions 获取数据
- modelica - 减小 Dymola 结果的 mat 文件大小
- python - 获取 eval 使用的变量
- docker - 无法在 docker 容器中将 nginx 从 1.18.0 升级到 1.20