【问题标题】:refresh button for datagridviewdatagridview 的刷新按钮
【发布时间】:2016-06-29 02:38:36
【问题描述】:

我需要帮助来刷新 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

我是编程新手谢谢你的帮助

【问题讨论】:

    标签: vb.net datagridview


    【解决方案1】:

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

    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. 在显示按钮的点击事件中调用方法填充网格:

        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 
      

    【讨论】:

    • 非常感谢 :) 但我还有另一个问题。 datagridview 的第一个视图没有关闭。它仍然存在并添加额外的视图
    • 对不起,我没听懂您的意思...您所说的第一视图是什么意思?
    • 它刷新我输入的更新。但 datagridview 中显示的旧数据仍然存在。
    • {5678 Guevarra Vince Andrew example456@gmail.com 456 1234 Gonzales Mike Stephen example123@gmail.com 123} {5678 Guevarra Vince Andrew example456@gmail.com 456 1234 Gonzales Christian Stephen example123@gmail.com 123}
    • 它是这样显示的。我将 Gonzales Mike Stephen 的名字更新为 Gonzales Christian Stephen。但是datagridview的最后一个数据仍然在Grid中
    猜你喜欢
    • 2015-09-03
    • 2017-01-17
    • 2015-04-06
    • 2019-04-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-30
    • 1970-01-01
    相关资源
    最近更新 更多