点击按钮更新PowerShell WinForm的DataGridView数据不生效问题咨询
问题根因
- 变量作用域问题:PowerShell函数内默认定义的是局部变量,你在
AddClient函数内直接给$tableData、$dataGridView赋值,修改的是函数内的局部变量,并不会影响界面上绑定的全局控件和数据源 - 错误重新实例化控件:你在刷新逻辑中重新创建了
DataGridView实例,但没有将新实例添加到窗体控件集合中,界面始终显示的是初始化时创建的旧控件,变更自然不会呈现 - 数据源更新不符合WinForm绑定规范:直接替换数据源变量不会触发控件自动刷新,需要主动重置控件的
DataSource属性 - 原有代码存在变量拼写错误:逻辑分支中使用了未定义的
$Usernames变量,会导致新增逻辑异常
修正后的可运行代码
Add-Type -AssemblyName System.Windows.Forms Add-Type -AssemblyName System.Data Add-Type -AssemblyName System.Collections $jsonDB = Get-Content 'C:\Support\HardwareCollection.json' -Raw | ConvertFrom-Json $script:tableData = New-Object System.Collections.ArrayList $tableData.AddRange($jsonDB) $Form = New-Object System.Windows.Forms.Form $Form.Size = New-Object System.Drawing.Size(1050, 425) $Form.MaximizeBox = $False $Form.StartPosition = "CenterScreen" $Form.FormBorderStyle = 'Fixed3D' $Form.Text = "Hardware Collection" $AddClientNButton = new-object System.Windows.Forms.Button $AddClientNButton.Location = new-object system.drawing.size(61,90) $AddClientNButton.Size = new-object system.drawing.size(80,50) $AddClientNButton.Text = "Add Client" $AddClientNButton.Add_Click({AddClient}) $AddClientNButton.TabIndex = 10 $Form.Controls.Add($AddClientNButton) $UserNametextBox = New-Object System.Windows.Forms.TextBox $UserNametextBox.Location = New-Object System.Drawing.Point(1,1) $UserNametextBox.Size = New-Object System.Drawing.Size(300,20) $UserNametextBox.text="User Name" $UserNametextBox.MaxLength = 6 $UserNametextBox.Add_Click({$this.SelectAll(); $this.Focus()}) $UserNametextBox.TabIndex = 0 $form.Controls.Add($UserNametextBox) $script:dataGridView = New-Object System.Windows.Forms.DataGridView -Property @{ Size = New-Object System.Drawing.Size(500, 200) Location = New-Object System.Drawing.Size(1, 150) ColumnHeadersVisible = $True DataSource = $tableData AutoSizeColumnsMode = 'AllCells' ColumnHeadersHeightSizeMode = 'AutoSize' } function AddClient { $jsonDB = Get-Content 'C:\Support\HardwareCollection.json' -Raw | ConvertFrom-Json $Date = Get-Date -Format "MM/dd/yyyy HH:mm:ss K" $Username = $UserNametextBox.text.Trim() if ([string]::IsNullOrEmpty($Username)) { return } $NewEntryItems = $null if($null -eq $jsonDB.username) { $NewEntryItems = $Username } else { if($jsonDB.username -notcontains $Username) { $NewEntryItems = $Username } } if ($null -ne $NewEntryItems) { $CollectionData = New-Object PSObject -Property @{ Username = $Username Date = $Date } $jsonDB += $CollectionData $jsonDB | ConvertTo-Json -Compress | Set-Content 'C:\Support\HardwareCollection.json' } # 刷新DataGridView逻辑 $newJson = Get-Content 'C:\Support\HardwareCollection.json' -Raw | ConvertFrom-Json $script:tableData.Clear() $script:tableData.AddRange($newJson) # 重置数据源触发刷新 $script:dataGridView.DataSource = $null $script:dataGridView.DataSource = $script:tableData } $Form.Controls.Add($dataGridView) $Form.ShowDialog()
核心修改说明
- 给全局使用的
$tableData、$dataGridView添加script:作用域前缀,确保函数内可以修改到全局对象 - 删除了函数中重新实例化
DataGridView的冗余逻辑,直接修改原有控件的数据源 - 修复了未定义变量的逻辑错误,新增了空值校验避免无效提交
- 刷新时先将数据源置空再重新赋值,强制触发DataGridView的界面刷新
内容的提问来源于stack exchange,提问作者Listor
相关产品推荐
相关产品推荐

