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.<\/p>\n
For Code I have:<\/p>\n
Import-Module -Name SqlServer\n\n Add-Type -AssemblyName System.Windows.Forms\n Add-Type -AssemblyName System.Data\n\n\n $form = New-Object System.Windows.Forms.Form\n $form.Text = \"Database Information\"\n $Form.Size = New-Object System.Drawing.Size(600,400)\n\n # Example: Add a DataGridView\n $dataGridView = New-Object System.Windows.Forms.DataGridView\n $dataGridView.Location = New-Object System.Drawing.Point(10, 10)\n $dataGridView.Size = New-Object System.Drawing.Size(400, 300)\n $form.Controls.Add($dataGridView)\n\n $connectionString = \"Data Source=MYPC\\SQLEXPRESS01;Initial Catalog=Visitors;Integrated Security=True;\"\n $sqlConnection = New-Object System.Data.SqlClient.SqlConnection($connectionString)\n $sqlCommand = New-Object System.Data.SqlClient.SqlCommand(\"SELECT * FROM VisitorInfo\", $sqlConnection)\n $sqlConnection.Open()\n $sqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter($sqlCommand)\n $dataTable = New-Object System.Data.DataTable\n $sqlAdapter.Fill($dataTable)\n $sqlConnection.Close()\n \n $dataGridView.DataSource = $dataTable\n\n########## TAB CONTROLS ##########\n $tabControl = New-Object System.Windows.Forms.TabControl\n #$tabControl.Dock = [System.Windows.Forms.DockStyle]::Fill \n $TabControl.Size = New-Object System.Drawing.Size(550, 320)\n $TabControl.Location = New-Object System.Drawing.Point(10, 10) \n $form.Controls.Add($tabControl)\n \n $tabPage1 = New-Object System.Windows.Forms.TabPage\n $tabPage1.Text = \"Tab 1\"\n $tabControl.Controls.Add($tabPage1)\n\n\n $tabPage2 = New-Object System.Windows.Forms.TabPage\n $tabPage2.Text = \"Tab 2\"\n $tabControl.Controls.Add($tabPage2)\n\n # Create textbox\n $DatabasetxtBox = New-Object System.Windows.Forms.TextBox\n $DatabasetxtBox.Location = New-Object System.Drawing.Point(120, 20)\n $DatabasetxtBox.Size = New-Object System.Drawing.Size(200, 20)\n\n $AddButton = New-Object System.Windows.Forms.Button\n $AddButton.Text = \"Add\"\n $AddButton.Location = New-Object System.Drawing.Point(425, 50)\n $AddButton.Size = New-Object System.Drawing.Size(100, 30)\n $tabPage2.Controls.Add($AddButton) # Adds the button to the first tab\n $AddButton.Add_Click({\n Invoke-Sqlcmd -ServerInstance \"MYPC\\SQLEXPRESS01\" -Database \"Visitors\" -Query \"INSERT INTO VisitorInfo ([FirstName], [LastName]) VALUES ('John','Cena');\" -TrustServerCertificate\n \n }) \n\n $DeleteButton = New-Object System.Windows.Forms.Button\n $DeleteButton.Text = \"Delete\"\n $DeleteButton.Location = New-Object System.Drawing.Point(425, 100)\n $DeleteButton.Size = New-Object System.Drawing.Size(100, 30)\n $tabPage2.Controls.Add($DeleteButton) # Adds the button to the first tab\n $DeleteButton.Add_Click({\n Invoke-Sqlcmd -ServerInstance \"MYPC\\SQLEXPRESS01\" -Database \"Visitors\" -Query \"SELECT * FROM VisitorInfo\" -TrustServerCertificate\n })\n \n $RefreshButton = New-Object System.Windows.Forms.Button\n $RefreshButton.Text = \"Refresh\"\n $RefreshButton.Location = New-Object System.Drawing.Point(425, 150)\n $RefreshButton.Size = New-Object System.Drawing.Size(100, 30)\n $tabPage2.Controls.Add($RefreshButton) # Adds the button to the first tab\n $RefreshButton.Add_Click({\n \n $DatabasetxtBox.text = $dataTable\n \n }) \n\n $tabPage2.Controls.Add($dataGridView)\n $form.ShowDialog() \n<\/code><\/pre>","upvoteCount":5,"answerCount":3,"datePublished":"2025-07-18T18:57:48.835Z","author":{"@type":"Person","name":"SpiceAddict85","url":"https://community.spiceworks.com/u/SpiceAddict85"},"acceptedAnswer":{"@type":"Answer","text":"
Advertisement
Note that I code a lot, but hardly ever in Powershell, so loose interpretation here.
\nI 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..<\/p>","upvoteCount":4,"datePublished":"2025-07-18T19:31:24.462Z","url":"https://community.spiceworks.com/t/powershell-refreshing-data-in-table/1225081/2","author":{"@type":"Person","name":"craigrrr","url":"https://community.spiceworks.com/u/craigrrr"}},"suggestedAnswer":[{"@type":"Answer","text":"
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.<\/p>\n
For Code I have:<\/p>\n
Import-Module -Name SqlServer\n\n Add-Type -AssemblyName System.Windows.Forms\n Add-Type -AssemblyName System.Data\n\n\n $form = New-Object System.Windows.Forms.Form\n $form.Text = \"Database Information\"\n $Form.Size = New-Object System.Drawing.Size(600,400)\n\n # Example: Add a DataGridView\n $dataGridView = New-Object System.Windows.Forms.DataGridView\n $dataGridView.Location = New-Object System.Drawing.Point(10, 10)\n $dataGridView.Size = New-Object System.Drawing.Size(400, 300)\n $form.Controls.Add($dataGridView)\n\n $connectionString = \"Data Source=MYPC\\SQLEXPRESS01;Initial Catalog=Visitors;Integrated Security=True;\"\n $sqlConnection = New-Object System.Data.SqlClient.SqlConnection($connectionString)\n $sqlCommand = New-Object System.Data.SqlClient.SqlCommand(\"SELECT * FROM VisitorInfo\", $sqlConnection)\n $sqlConnection.Open()\n $sqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter($sqlCommand)\n $dataTable = New-Object System.Data.DataTable\n $sqlAdapter.Fill($dataTable)\n $sqlConnection.Close()\n \n $dataGridView.DataSource = $dataTable\n\n########## TAB CONTROLS ##########\n $tabControl = New-Object System.Windows.Forms.TabControl\n #$tabControl.Dock = [System.Windows.Forms.DockStyle]::Fill \n $TabControl.Size = New-Object System.Drawing.Size(550, 320)\n $TabControl.Location = New-Object System.Drawing.Point(10, 10) \n $form.Controls.Add($tabControl)\n \n $tabPage1 = New-Object System.Windows.Forms.TabPage\n $tabPage1.Text = \"Tab 1\"\n $tabControl.Controls.Add($tabPage1)\n\n\n $tabPage2 = New-Object System.Windows.Forms.TabPage\n $tabPage2.Text = \"Tab 2\"\n $tabControl.Controls.Add($tabPage2)\n\n # Create textbox\n $DatabasetxtBox = New-Object System.Windows.Forms.TextBox\n $DatabasetxtBox.Location = New-Object System.Drawing.Point(120, 20)\n $DatabasetxtBox.Size = New-Object System.Drawing.Size(200, 20)\n\n $AddButton = New-Object System.Windows.Forms.Button\n $AddButton.Text = \"Add\"\n $AddButton.Location = New-Object System.Drawing.Point(425, 50)\n $AddButton.Size = New-Object System.Drawing.Size(100, 30)\n $tabPage2.Controls.Add($AddButton) # Adds the button to the first tab\n $AddButton.Add_Click({\n Invoke-Sqlcmd -ServerInstance \"MYPC\\SQLEXPRESS01\" -Database \"Visitors\" -Query \"INSERT INTO VisitorInfo ([FirstName], [LastName]) VALUES ('John','Cena');\" -TrustServerCertificate\n \n }) \n\n $DeleteButton = New-Object System.Windows.Forms.Button\n $DeleteButton.Text = \"Delete\"\n $DeleteButton.Location = New-Object System.Drawing.Point(425, 100)\n $DeleteButton.Size = New-Object System.Drawing.Size(100, 30)\n $tabPage2.Controls.Add($DeleteButton) # Adds the button to the first tab\n $DeleteButton.Add_Click({\n Invoke-Sqlcmd -ServerInstance \"MYPC\\SQLEXPRESS01\" -Database \"Visitors\" -Query \"SELECT * FROM VisitorInfo\" -TrustServerCertificate\n })\n \n $RefreshButton = New-Object System.Windows.Forms.Button\n $RefreshButton.Text = \"Refresh\"\n $RefreshButton.Location = New-Object System.Drawing.Point(425, 150)\n $RefreshButton.Size = New-Object System.Drawing.Size(100, 30)\n $tabPage2.Controls.Add($RefreshButton) # Adds the button to the first tab\n $RefreshButton.Add_Click({\n \n $DatabasetxtBox.text = $dataTable\n \n }) \n\n $tabPage2.Controls.Add($dataGridView)\n $form.ShowDialog() \n<\/code><\/pre>","upvoteCount":5,"datePublished":"2025-07-18T18:57:48.907Z","url":"https://community.spiceworks.com/t/powershell-refreshing-data-in-table/1225081/1","author":{"@type":"Person","name":"SpiceAddict85","url":"https://community.spiceworks.com/u/SpiceAddict85"}},{"@type":"Answer","text":"