Q:

refresh button for datagridview

i need help in refresh the datagridview. I create a Update button and show button but it didn't refresh. but i check in my database access it update successful
Imports System.Data
Imports System.Data.OleDb
Public Class Form2
Public con As OleDbConnection
Public da As OleDbDataAdapter
Public ds As New DataSet
Public cmd As OleDbCommand
Public dr As OleDbDataReader
Dim count As Integer = 100
Dim ID As String

Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    Try
        'Establish connection
        con = New OleDbConnection("Provider = Microsoft.ACE.OLEDB.12.0; Data Source = C:\Users\m_j_g\Documents\Visual Studio 2010\Projects\DatabaseApplication\Department.accdb")
    Catch ex As Exception
        MsgBox(ex.Message)
    End Try
End Sub

Private Sub btnShow_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnShow.Click
    Try
        'establish connection
        con = New OleDbConnection("provider = Microsoft.ACE.OLEDB.12.0; data source = C:\Users\m_j_g\OneDrive\Documents\Database1.accdb")
        'execute sql query
        da = New OleDbDataAdapter("Select * from table1", con)
        'Fill Dataset
        da.Fill(ds, "table1")
        'put data from dataset to datagridview
        DataGridView1.DataSource = ds.Tables(0)
    Catch ex As Exception
        MsgBox(ex.Message)
    End Try

End Sub
Private Sub btnUpdate_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnUpdate.Click
    con = New OleDbConnection("provider = Microsoft.ACE.OLEDB.12.0; data source = C:\Users\m_j_g\OneDrive\Documents\Database1.accdb")

    Try
        con.Open()
        cmd = New OleDbCommand("Update table1 set SurName='" & TextBox2.Text & "', FirstName='" & TextBox3.Text & "' where EmailAddress='" & TextBox5.Text & "'", con)
        'Execute UPDATE Query
        cmd.ExecuteNonQuery()
        MsgBox("Record Updated Successfully..." & Chr(13) & "Email Address: " & TextBox5.Text & Chr(13) & "SurName: " & TextBox2.Text & Chr(13) & "FirstName: " & TextBox3.Text)
        con.Close()
    Catch ex As Exception
        MsgBox(ex.Message)
    End Try
End Sub

I am newbie in programming Thank you for your help

A:

You need to rebind the Grid after performing the update, Follow these steps this may help you:

Define a Method for binding the giridiew, this will populate the grid with values from the DB:
Public Sub bindMyGrid()
   con = New OleDbConnection("provider = Microsoft.ACE.OLEDB.12.0; data source = C:\Users\m_j_g\OneDrive\Documents\Database1.accdb")
   da = New OleDbDataAdapter("Select * from table1", con)
   da.Fill(ds, "table1")
   DataGridView1.Rows.Clear() '<-- new line added
   DataGridView1.DataSource = ds.Tables(0)
End Sub

Call the method in the click event of the show button to populate the grid:
  Private Sub btnShow_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnShow.Click
    Try
       bindMyGrid()
    Catch ex As Exception
    ' handle error here 
    End Try
  End Sub

Perform the update operation on update button click, and call the method again to get the latest values.
  Private Sub btnUpdate_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnUpdate.Click
      Try
        'Perform the update operations here 
        ' Call  bindMyGrid() to rebind the grid
        bindMyGrid() 'this will populate the new value to the grid.
      Catch ex As Exception
        'Handle error here
      End Try
  End Sub 

