我是VB的新手。当我从frmAddBook添加数据时,我想在我的frmSearchUpdateBook上刷新我的DataGridView。添加数据后,我的frmSearchUpdateBook上的DataGridView不会刷新,添加的数据也不存在,需要重新运行应用程序才能查看新添加的数据。有人可以帮帮我吗?
这是我的代码:
Imports System.Data.OleDb
公共类frmAddBooks
Private Sub btnSave_Click(sender as Object,e As EventArgs)处理btnSave.Click
Dim cn As New OleDbConnection
Dim cm As New OleDbCommand
Try
With cn
.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\Assessment of the CKC Library System\Database.accdb"
.Open()
End With
With cm
.Connection = cn
.CommandType = CommandType.Text
.CommandText = "INSERT INTO [Books] ([ISBN],[BookTitle],[Author],[Category],[Foreword],[YearPublished], [Quantity]) VALUES ('" & txtISBN.Text & "', '" & txtBookTitle.Text & "', '" & txtAuthor.Text & "', '" & cboCategory.Text & "', '" & txtForeword.Text & "', '" & cboYearPublished.Text & "', '" & cboQuantity.Text & "')"
.Parameters.Add(New System.Data.OleDb.OleDbParameter("@ISBN", System.Data.OleDb.OleDbType.VarChar, 30, Me.txtISBN.Text))
.Parameters.Add(New System.Data.OleDb.OleDbParameter("@BookTitle", System.Data.OleDb.OleDbType.VarChar, 255, Me.txtBookTitle.Text))
.Parameters.Add(New System.Data.OleDb.OleDbParameter("@Author", System.Data.OleDb.OleDbType.VarChar, 255, Me.txtAuthor.Text))
.Parameters.Add(New System.Data.OleDb.OleDbParameter("@Category", System.Data.OleDb.OleDbType.VarChar, 255, Me.cboCategory.Text))
.Parameters.Add(New System.Data.OleDb.OleDbParameter("@Foreword", System.Data.OleDb.OleDbType.VarChar, 255, Me.txtForeword.Text))
.Parameters.Add(New System.Data.OleDb.OleDbParameter("@YearPublished", System.Data.OleDb.OleDbType.VarChar, 255, Me.cboYearPublished.Text))
.Parameters.Add(New System.Data.OleDb.OleDbParameter("@Quantity", System.Data.OleDb.OleDbType.VarChar, 255, Me.cboQuantity.Text))
'RUNS THE COMMAND
cm.Parameters("@ISBN").Value = Me.txtISBN.Text
cm.Parameters("@BookTitle").Value = Me.txtBookTitle.Text
cm.Parameters("@Author").Value = Me.txtAuthor.Text
cm.Parameters("@Category").Value = Me.cboCategory.Text
cm.Parameters("@Foreword").Value = Me.txtForeword.Text
cm.Parameters("@YearPublished").Value = Me.cboYearPublished.Text
cm.Parameters("@Quantity").Value = Me.cboQuantity.Text
cm.ExecuteNonQuery()
MsgBox("Book added.", MsgBoxStyle.Information, "Saved Successfully!")
Me.txtISBN.Text = ""
Me.txtBookTitle.Text = ""
Me.txtAuthor.Text = ""
Me.txtForeword.Text = ""
cboCategory.SelectedIndex = -1
cboQuantity.SelectedIndex = -1
cboYearPublished.SelectedIndex = -1
Exit Sub
End With
Catch ex As Exception
MsgBox("Please fill all the details needed and be sure that the ISBN doesn't have any letters. ", MsgBoxStyle.Exclamation, "Error")
End Try
End Sub
Private Sub btnCancel_Click(sender As Object, e As EventArgs) Handles btnCancel.Click
Hide()
End Sub
结束班
公共类frmSearchUpdateBooks
Private Sub BooksBindingNavigatorSaveItem_Click(sender As Object, e As EventArgs)
Me.Validate()
Me.BooksBindingSource.EndEdit()
Me.TableAdapterManager.UpdateAll(Me.DatabaseDataSet)
End Sub
Private Sub frmSearchUpdateBooks_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the 'DatabaseDataSet.Books' table. You can move, or remove it, as needed.
Me.BooksTableAdapter.Fill(Me.DatabaseDataSet.Books)
End Sub
Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
Try
BooksBindingSource.EndEdit()
BooksTableAdapter.Update(DatabaseDataSet)
MsgBox("Successfully saved.", MsgBoxStyle.OkOnly, "Success!")
DataGridView1.Refresh()
Catch ex As Exception
MsgBox("Please fill all the information needed. If all informations are filled up, please re-check the ISBN as some book might have the same ISBN as you are entering.", MsgBoxStyle.OkOnly, "Error!")
End Try
End Sub
Private Sub btnDelete_Click(sender As Object, e As EventArgs) Handles btnDelete.Click
BooksBindingSource.RemoveCurrent()
BooksBindingSource.EndEdit()
MsgBox("Item successfully deleted!", MsgBoxStyle.OkOnly, "Success!")
End Sub
Private Sub btnUpdate_Click(sender As Object, e As EventArgs) Handles btnUpdate.Click
Try
Me.Validate()
Me.BooksBindingSource.EndEdit()
Me.BooksTableAdapter.Update(DatabaseDataSet)
MsgBox("Updated Successfully!", MsgBoxStyle.OkOnly, "Update Successful!")
Catch ex As Exception
MsgBox(ex.Message)
Finally
Me.BooksTableAdapter.Fill(DatabaseDataSet.Books)
End Try
End Sub
结束班
如果我只使用1个表单,我可以更新DataGridView,但我的教授要我使用2个表单。一个用于添加书籍,一个用于数据网格浏览。谢谢!
答案 0 :(得分:0)
以下是代码建议:
更改负载以使用单独的方法调用书籍的负载。然后,保存后将调用相同的方法。只需用这些方法替换你的同名方法。
Private Sub frmSearchUpdateBooks_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.LoadBooks()
End Sub
Private Sub LoadBooks()
Me.BooksTableAdapter.Fill(Me.DatabaseDataSet.Books)
Me.DataGridView1.DataSource = Me.DatabaseDataSet.Books
End Sub
Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
Try
BooksBindingSource.EndEdit()
BooksTableAdapter.Update(DatabaseDataSet)
MsgBox("Successfully saved.", MsgBoxStyle.OkOnly, "Success!")
Me.LoadBooks()
Catch ex As Exception
MsgBox("Please fill all the information needed. If all informations are filled up, please re-check the ISBN as some book might have the same ISBN as you are entering.", MsgBoxStyle.OkOnly, "Error!")
End Try
End Sub