簡體   English   中英

如何使用2種表單重新加載DataGridView? Visual Basic 2015

[英]How can I reload my DataGridView using 2 forms? Visual Basic 2015

我是VB新手。 當我從frmAddBook添加數據時,我想在frmSearchUpdateBook上刷新DataGridView。 添加數據后,不會刷新我的frmSearchUpdateBook上的DataGridView並且添加的數據也不在那里,它需要重新運行該應用程序以查看新添加的數據。 有人可以幫我嗎?

這是我的代碼:

導入System.Data.OleDb

公共類frmAddBooks

私有Sub btnSave_Click(發送者為對象,e作為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個表單,但我的教授希望我使用2個表單,則可以更新DataGridView。 一本用於添加書籍,一本用於datagridviewing。 謝謝!

這是推薦的代碼:

更改負載以使用單獨的方法調用書籍的負載。 然后,保存后將調用相同的方法。 只需將您具有相同名稱的方法替換為這些方法即可。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM