How do i refresh my datagridview after inserting new SQL data using vb.net

25
February 28, 2019, at 4:20 PM

To elaborate my project i have 4 forms the 1st one is the main, i used panel to switch from the other 3 forms, although the inserting of data is working i want to see the updated sql column in the table after i inserted from the other form but when i tried to refresh the datagrid it doesn't work i have to close the program and start again just to load the new data in the datagridview.

I've tried:

DataGridView1.Refresh();

and load_table() but it doesn't work for me.

This is the code that display the tables on my database(2nd form):

Imports MySql.Data.MySqlClient
Public Class search
    Dim cn As New MySqlConnection
    Dim COMMAND As MySqlCommand


    Public Sub DataGridView1_ParentChanged(sender As Object, e As EventArgs) Handles DataGridView1.ParentChanged
        cn.ConnectionString = "server=localhost; userid=root; password=4102123; database=pis"
        Dim SDA As New MySqlDataAdapter
        Dim dbDataSet As New DataTable
        Dim bSource As New BindingSource
        Try
            cn.Open()
            Dim Query As String
            Query = "select * from pis.patient_info"
            COMMAND = New MySqlCommand(Query, cn)
            SDA.SelectCommand = COMMAND
            SDA.Fill(dbDataSet)
            bSource.DataSource = dbDataSet
            DataGridView1.DataSource = bSource
            SDA.Update(dbDataSet)
            cn.Close()
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

End Class

This is the main code(1st form):

Imports MySql.Data.MySqlClient
Public Class Form1
    Dim cn As New MySqlConnection
    Dim COMMAND As MySqlCommand
    Private Sub OvalShape1_Click(sender As Object, e As EventArgs)
    End Sub
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    End Sub
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        switchpanel(search)
    End Sub
    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        switchpanel(add)
    End Sub
    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        switchpanel(del)
    End Sub
    Sub switchpanel(ByVal panel As Form)
        Panel1.Controls.Clear()
        panel.TopLevel = False
        Panel1.Controls.Add(panel)
        panel.Show()
    End Sub
End Class

Code i use for insertion of data(3rd form):

Imports MySql.Data.MySqlClient
Public Class add
    Dim cn As New MySqlConnection
    Dim COMMAND As MySqlCommand

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        cn.ConnectionString = "server=localhost; userid=root; password=4102123; database=pis;"
        Dim READER As MySqlDataReader
        Try
            cn.Open()
            Dim Query As String
            Query = "insert into pis.patient_info(Name) values('" & TextBox1.Text & "')"
            COMMAND = New MySqlCommand(Query, cn)
            READER = COMMAND.ExecuteReader
            cn.Close()
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

End Class

Is it possible to call another form or class on different forms? For example: from the main form above i'll call the 2nd form to execute the code again, because once i click the button from the main the program code only execute once, when i switch forms to 3rd and 1st form then come back to 2nd form the datagridview will not refresh anymore, instead the code that will work only is the switchpanel(search) and displays not updated datagridview.

READ ALSO
Create a MySQL query to calculate the total price of an order [on hold]

Create a MySQL query to calculate the total price of an order [on hold]

I've developed an ERD with customers/orders/products, but now I'm struggling creating queries for it

20
how can I make it so only the logged in user view his own grade?

how can I make it so only the logged in user view his own grade?

This is the student info table where the student info comes from //This is the login code for the user and it get the user log in info from the studentinfo table

19
#1062 - Duplicate entry '19' for key 'PRIMARY'

#1062 - Duplicate entry '19' for key 'PRIMARY'

I have the following table in MySQL version 55

32