使用vb.net插入新SQL数据后如何刷新datagridview

时间:2019-02-27 19:19:13

标签: mysql sql vb.net visual-studio

为了详细说明我的项目,我有4种形式,第一种是主要形式,我使用了面板 可以从其他三种形式切换,尽管可以插入数据 我想从其他表格插入后在表格中看到更新的sql列,但是当我尝试刷新datagrid时,它不起作用,我必须关闭程序并再次开始,只是将新数据加载到datagridview中。

我尝试过:

DataGridView1.Refresh();

load_table(),但对我不起作用。

这是在我的数据库(第二种形式)上显示表格的代码:

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

这是主要代码(第一种形式):

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

我用于插入数据的代码(第三种形式):

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

是否可以在其他表单上调用其他表单或类? 例如:从上面的主窗体中,我将调用第二个窗体来执行代码 再次,因为一旦我从主菜单中单击按钮,仅程序代码 执行一次,当我将表格切换到第三和第一表格,然后回到第二表格 datagridview将不再刷新,而是仅适用的代码 是switchpanel(search),并显示未更新的datagridview。

0 个答案:

没有答案