Vb.Net update Datagridview in main form when save data in another form

Viewed 468

This my function to display data from sql server to Datagridview

 Private Function getpdfinfo1() As DataTable
    DataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect

    Dim dtpi As New DataTable
    Dim connString As String = ConfigurationManager.ConnectionStrings("pdflib.My.MySettings.hazimdbConnectionString").ConnectionString

    Using conn As New SqlConnection(connString)
        Using cmd As New SqlCommand("select idpdf as  ID,pdfname as  Title, categories as Categories , extension as Extension from pdfinfo where ( username='" & Label10.Text & "' ) ", conn)
            conn.Open()
            Dim reader As SqlDataReader = cmd.ExecuteReader()
            dtpi.Load(reader)
            conn.Close()
        End Using
    End Using
    Return dtpi

End Function

And in Main Form Load even I am calling the function getpdfinfo1() as shown below

Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        DataGridView1.DataSource = getpdfinfo1()
End Sub

I save data from another form. The button click event to save data is as shown below

 cmd = New SqlCommand("Insert into pdfinfo (pdfname,pdfdata,categories,username,iduser,extension) values (@pdfname,@pdfdata,@categories,@username,@iduser,@extension) ", connection)
        connection.Open()
        cmd.Parameters.Add(New SqlParameter("@pdfname", SqlDbType.NVarChar, 100)).Value = TextBox2.Text
        cmd.Parameters.Add(New SqlParameter("@categories", SqlDbType.NVarChar, 100)).Value = ComboBox1.Text
        cmd.Parameters.Add(New SqlParameter("@username", SqlDbType.NVarChar, 100)).Value = TextBox4.Text
        cmd.Parameters.Add(New SqlParameter("@iduser", SqlDbType.Int)).Value = Label6.Text
        cmd.Parameters.Add(New SqlParameter("@extension", SqlDbType.NChar, 10)).Value = Path.GetExtension(ofd.FileName)
        Dim fs As New FileStream(ofd.FileName, FileMode.Open, FileAccess.Read)
        Dim br As New BinaryReader(fs)
        Dim file() As Byte = br.ReadBytes(br.BaseStream.Length)
        cmd.Parameters.Add(New SqlParameter("@pdfdata", SqlDbType.VarBinary)).Value = file
        cmd.ExecuteNonQuery()
        Label8.Text = "Upload Completed"

So I want when clicked Save button in (save data Form) then the Datagridview** in Main Form (Form 2) will be updated automatically.

Or, How to create function update Datagridview when Sql data changes.

2 Answers

The getpdfinfo1 function is private, make it public function and call this function in the save button click event. To call the functions from other form you can just prefix the form name.function.

  1. Make a dataclass containing all the data you want to modify in Form 2
  2. Declare a dataclass property (friend or public) in form 2
  3. Refresh/fill the dataclass when pressing Save on Form 2
  4. Set From 2 Save-button as acceptbutton
  5. Call Form 2 from the Main Form an check on dialogresult OK
  6. When dialogresult = OK Refresh datagridview / save data using sql/...
  7. Be carefull if you don't want to write/read sql first to refresh your dgv, that your sql transaction was completed succesfull. This is imortant to prevent users would see different data in the datagridview than there is in your database.
Related