datagridview的刷新按钮

时间:2016-03-14 06:43:46

标签: vb.net datagridview

我在刷新datagridview时需要帮助。我创建了一个更新按钮和显示按钮,但它没有刷新。但我检查我的数据库访问它更新成功

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

我是编程新手感谢您的帮助

1 个答案:

答案 0 :(得分:0)

执行更新后需要重新绑定网格,按照以下步骤操作可能会对您有所帮助:

  1. 定义一个绑定giridiew的方法,这将使用来自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
    
  2. 在show按钮的click事件中调用方法以填充网格:

      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
    
  3. 单击更新按钮执行更新操作,再次调用该方法以获取最新值。

      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