I am trying to build a powershell gui (for practice) with tabs and the one tab I am working with in this situation, has a datagridview on it, which I want to have it pull data from a database. I wanted to have the buttons; add, fresh, delete. I got the add button to work. Now I am working on the refresh button but with all the research I have done, i still can’t get it to work. I guess I am not understanding the construct of the refresh. I thought I could just reference the .refresh() method of the datagridview to accomplish the fresh of the data in the DB table.

For Code I have:

Import-Module -Name SqlServer

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


    $form = New-Object System.Windows.Forms.Form
    $form.Text = "Database Information"
    $Form.Size = New-Object System.Drawing.Size(600,400)

    # Example: Add a DataGridView
    $dataGridView = New-Object System.Windows.Forms.DataGridView
    $dataGridView.Location = New-Object System.Drawing.Point(10, 10)
    $dataGridView.Size = New-Object System.Drawing.Size(400, 300)
    $form.Controls.Add($dataGridView)

    $connectionString = "Data Source=MYPC\SQLEXPRESS01;Initial Catalog=Visitors;Integrated Security=True;"
    $sqlConnection = New-Object System.Data.SqlClient.SqlConnection($connectionString)
    $sqlCommand = New-Object System.Data.SqlClient.SqlCommand("SELECT * FROM VisitorInfo", $sqlConnection)
    $sqlConnection.Open()
    $sqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter($sqlCommand)
    $dataTable = New-Object System.Data.DataTable
    $sqlAdapter.Fill($dataTable)
    $sqlConnection.Close()
    
    $dataGridView.DataSource = $dataTable

########## TAB CONTROLS ##########
    $tabControl = New-Object System.Windows.Forms.TabControl
    #$tabControl.Dock = [System.Windows.Forms.DockStyle]::Fill 
    $TabControl.Size = New-Object System.Drawing.Size(550, 320)
    $TabControl.Location = New-Object System.Drawing.Point(10, 10)      
    $form.Controls.Add($tabControl)
    
    $tabPage1 = New-Object System.Windows.Forms.TabPage
    $tabPage1.Text = "Tab 1"
    $tabControl.Controls.Add($tabPage1)


    $tabPage2 = New-Object System.Windows.Forms.TabPage
    $tabPage2.Text = "Tab 2"
    $tabControl.Controls.Add($tabPage2)

  # Create textbox
    $DatabasetxtBox                    = New-Object System.Windows.Forms.TextBox
    $DatabasetxtBox.Location           = New-Object System.Drawing.Point(120, 20)
    $DatabasetxtBox.Size               = New-Object System.Drawing.Size(200, 20)

 $AddButton = New-Object System.Windows.Forms.Button
    $AddButton.Text = "Add"
    $AddButton.Location = New-Object System.Drawing.Point(425, 50)
    $AddButton.Size = New-Object System.Drawing.Size(100, 30)
    $tabPage2.Controls.Add($AddButton) # Adds the button to the first tab
    $AddButton.Add_Click({
        Invoke-Sqlcmd -ServerInstance "MYPC\SQLEXPRESS01" -Database "Visitors" -Query "INSERT INTO VisitorInfo ([FirstName], [LastName]) VALUES ('John','Cena');"  -TrustServerCertificate
        
    })    

    $DeleteButton = New-Object System.Windows.Forms.Button
    $DeleteButton.Text = "Delete"
    $DeleteButton.Location = New-Object System.Drawing.Point(425, 100)
    $DeleteButton.Size = New-Object System.Drawing.Size(100, 30)
    $tabPage2.Controls.Add($DeleteButton) # Adds the button to the first tab
    $DeleteButton.Add_Click({
        Invoke-Sqlcmd -ServerInstance "MYPC\SQLEXPRESS01" -Database "Visitors" -Query "SELECT * FROM VisitorInfo"  -TrustServerCertificate
    })
    
    $RefreshButton = New-Object System.Windows.Forms.Button
    $RefreshButton.Text = "Refresh"
    $RefreshButton.Location = New-Object System.Drawing.Point(425, 150)
    $RefreshButton.Size = New-Object System.Drawing.Size(100, 30)
    $tabPage2.Controls.Add($RefreshButton) # Adds the button to the first tab
    $RefreshButton.Add_Click({
            
            $DatabasetxtBox.text = $dataTable
     
    })    

    $tabPage2.Controls.Add($dataGridView)
    $form.ShowDialog()  
5 Spice ups

Note that I code a lot, but hardly ever in Powershell, so loose interpretation here.
I think the problem is your connection is already closed (aside from the fact that I do not see any actual call to refresh the datagridview). But re-establish the connection and update $datatable first, and then things might work more like expected. Oh, also clear the datagridview datasource first like $dataGridView.DataSource = $null, then re-establish connection, repopulate the table, reassign as the datasource. Or thereabouts..

4 Spice ups

@craigrrr that worked! Thank you for the assistance.

4 Spice ups