这是图书馆管理系统中模块(更新书的详细信息)的一部分。但是当我更改细节并单击更新按钮时,更新的数据成功存储在数据库中,但更新的数据不会同时在数据网格中刷新。
Imports System.Data
Imports System.Data.OleDb
Imports System.Data.Odbc
Imports System.Data.DataTable
Public Class frmBooksUpdate
Dim myConnection As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\LMS_DB.accdb")
Dim con As OleDbConnection
Dim provider As String
Dim dataFile As String
Dim connString As String
Dim ds As DataSet = New DataSet
Dim da As OleDbDataAdapter
Dim tables As DataTableCollection = ds.Tables
Dim source1 As New BindingSource()
Private Sub Refreshdata()
da = New OleDbDataAdapter("SELECT [BookID], [Title], [Author], [Publisher], [Category], [Price], [ISBN] FROM tblBooks", myConnection)
da.Fill(ds, "tblBooks")
Dim view1 As New DataView(tables(0))
source1.DataSource = view1
DataGridView1.DataSource = view1
DataGridView1.Refresh()
End Sub
Private Sub frmBookSearch_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Refreshdata()
End Sub
Private Sub DataGridView1_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellClick
Dim i As Integer
i = DataGridView1.CurrentRow.Index
txtBookID.Text = ds.Tables(0).Rows(i).Item(0)
txtTitle.Text = ds.Tables(0).Rows(i).Item(1)
txtAuthor.Text = ds.Tables(0).Rows(i).Item(2)
txtPublisher.Text = ds.Tables(0).Rows(i).Item(3)
txtCategory.Text = ds.Tables(0).Rows(i).Item(4)
txtPrice.Text = ds.Tables(0).Rows(i).Item(5)
txtISBN.Text = ds.Tables(0).Rows(i).Item(6)
End Sub
Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
myConnection.Open()
Dim str As String
str = "UPDATE [tblBooks] SET [Title] = '" & txtTitle.Text & "' , [Author] = '" & txtAuthor.Text & "', [Publisher] = '" & txtPublisher.Text & "', [Category] = '" & txtCategory.Text & "', [Price] = '" & txtPrice.Text & "', [ISBN] = '" & txtISBN.Text & "' WHERE [BookID] = " & txtBookID.Text & ""
Dim cmd As OleDbCommand = New OleDbCommand(str, myConnection)
Try
cmd.ExecuteNonQuery()
cmd.Dispose()
myConnection.Close()
MessageBox.Show("The Book Updated", "Book Updated", MessageBoxButtons.OK, MessageBoxIcon.Information)
txtAuthor.Clear()
txtBookID.Clear()
txtCategory.Clear()
txtISBN.Clear()
txtPrice.Clear()
txtPublisher.Clear()
txtTitle.Clear()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
Me.Close()
End Sub
End Class
答案 0 :(得分:0)
在MsgBox.Show
答案 1 :(得分:0)
将RefreshData()
放入更新活动........
Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
myConnection.Open()
Dim str As String
str = "UPDATE [tblBooks] SET [Title] = '" & txtTitle.Text & "' , [Author] = '" & txtAuthor.Text & "', [Publisher] = '" & txtPublisher.Text & "', [Category] = '" & txtCategory.Text & "', [Price] = '" & txtPrice.Text & "', [ISBN] = '" & txtISBN.Text & "' WHERE [BookID] = " & txtBookID.Text & ""
Dim cmd As OleDbCommand = New OleDbCommand(str, myConnection)
Try
cmd.ExecuteNonQuery()
cmd.Dispose()
myConnection.Close()
MessageBox.Show("The Book Updated", "Book Updated", MessageBoxButtons.OK, MessageBoxIcon.Information)
txtAuthor.Clear()
txtBookID.Clear()
txtCategory.Clear()
txtISBN.Clear()
txtPrice.Clear()
txtPublisher.Clear()
txtTitle.Clear()
RefreshData()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub