我目前正试图通过按钮单击更新我的datagridview,通过文本框添加用户并单击按钮,我希望datagridview更新以反映数据库的当前状态。我在软件启动时显示了datagridview,但是当我单击添加按钮并尝试刷新它时,数据库不显示。
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Data
Add-Type -AssemblyName System.Collections
$jsonDB = Get-Content 'C:SupportHardwareCollection.json' | Out-String | ConvertFrom-Json
#JsonDb has $JsonDB.update and $JsonDB.Date
$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)
$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:SupportHardwareCollection.json' | Out-String | ConvertFrom-Json
$json.username
$Date = Get-Date -Format "MM/dd/yyyy HH:mm:ss K"
$Username = $UserNametextBox.text
if($jsonDB.username -eq $null)
{
$NewEntryItems=$Usernames
}
else
{
if($jsonDB.username -contains $Username)
{
$NewEntryItems =$null
}
else
{
$NewEntryItems = $username
}
}
$CollectionData = ForEach ($Username in $NewEntryItems)
{
New-Object PSObject -Property @{
Username = $Username
Date = $Date
}
}
$jsonDB += $CollectionData
$jsonDB | ConvertTo-Json -Compress |
Set-Content C:SupportHardwareCollection.json
#refresh datagridview
$dataGridView.DataBindings.DefaultDataSourceUpdateMode = 0
$jsonDB = Get-Content 'C:SupportHardwareCollection.json' | Out-String | ConvertFrom-Json
#$dataGridView.DataSource = $jsonDB
$tableData = New-Object System.Collections.ArrayList
$tableData.AddRange($jsonDB)
$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'
}
}
$Form.Controls.Add($dataGridView)
$Form.ShowDialog()
任何帮助将是惊人的!
我在另一页上找到了答案。
$script:datagridview.DataBindings.DefaultDataSourceUpdateMode = 0
$script:datagridview.DataSource= $script:TableData
$script:datagridview.Refresh
引用页