1

I am currently trying to update my datagridview via button click, add a user via text box and clicking a button i want the datagridview to update to reflect the current stat of the database. I have the datagridview showing up on launch of the software, but when I click the add button and try to refresh it, the database does not show.

Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Data
Add-Type -AssemblyName System.Collections

$jsonDB = Get-Content 'C:\Support\HardwareCollection.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:\Support\HardwareCollection.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:\Support\HardwareCollection.json
    
    #refresh datagridview
    $dataGridView.DataBindings.DefaultDataSourceUpdateMode = 0
    $jsonDB = Get-Content 'C:\Support\HardwareCollection.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()

Any help would be amazing!!

1 Answer 1

0

I found the answer on another page. dumping this in fixed my issue.

    $script:datagridview.DataBindings.DefaultDataSourceUpdateMode = 0 
    $script:datagridview.DataSource= $script:TableData
    $script:datagridview.Refresh

Referenced Page

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.