LuckyLuke82 LuckyLuke82 - 15 days ago 8
Vb.net Question

Datagridview - refresh after DB Update on another form

I'm setting Datagridview DataSource in Form1. In Form 2 I update those records (works fine in DB), and after updating them I want to refresh Datagridview in Form1 too.

Here is my code in Form1_Button_Click:

OracleConn() 'connection to my DB

Using cmd As New OracleCommand()

Dim SQL As String = "Select * FROM MyTable"

If Chk1.Checked = True Then

cmd.Connection = OracleConn()
cmd.CommandText = SQL
cmd.CommandType = CommandType.Text

Dim dt1 As New DataTable
Using dad As New OracleDataAdapter(SQL, OracleConn)
dad.Fill(dt1)
End Using
End if
DataGridView1.DataSource = dt1

End Using


How can I do this most easily ?

Answer

Write down a method to fill your data grid in Form1

public sub FillData()
OracleConn() 'connection to my DB

    Using cmd As New OracleCommand()

     Dim SQL As String = "Select * FROM MyTable"

        If Chk1.Checked = True Then

            cmd.Connection = OracleConn()
            cmd.CommandText = SQL
            cmd.CommandType = CommandType.Text

            Dim dt1 As New DataTable
              Using dad As New OracleDataAdapter(SQL, OracleConn)
                dad.Fill(dt1)
              End Using
        End if
            DataGridView1.DataSource = dt1

 End Using
 end sub

Call the method into your button click ( Form1_Button_Click)

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
FillData() 
End sub

Now you can call the method of Form1 from Form2 directly after update,

Form1.FillData() 

Records will reflect in Form 1

NB: Method should be public in access.

Comments