2016-03-25 15 views
0

データセットに新しい行を追加すると、データグリッドビューの特定のフォームに表示されますが、同じデータバインドのDataGridviewを使用して別のフォームに切り替えると、新しい行は表示されません。プログラムを閉じると、新しい行が完全に消えてしまいます。私はそれが読んでいるAccessデータベースに新しい行を保存したい。データセットの変更が保存されません

Public Class frmAddStudent 




Private Sub btnBack_Click(sender As Object, e As EventArgs) Handles btnBack.Click 
    Me.Hide() 
    frmUserControls.Show() 
End Sub 

Private Sub frmAddStudent_Load(sender As Object, e As EventArgs) Handles MyBase.Load 
    'TODO: This line of code loads data into the 'StudentRecords1DataSet.tblLecturer' table. You can move, or remove it, as needed. 
    Me.TblLecturerTableAdapter.Fill(Me.StudentRecords1DataSet.tblLecturer) 
    'TODO: This line of code loads data into the 'StudentRecords1DataSet.tblStudents' table. You can move, or remove it, as needed. 
    Me.TblStudentsTableAdapter.Fill(Me.StudentRecords1DataSet.tblStudents) 

End Sub 

Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click 

    Dim MyNewRow As DataRow 
    MyNewRow = StudentRecords1DataSet.tblStudents.NewRow 



    Try 

     With MyNewRow 
      .Item(1) = txtID.Text 
      .Item(2) = txtFirstName.Text 
      .Item(3) = txtSurname.Text 


     End With 



     Me.Validate() 
     Me.TblStudentsBindingSource.EndEdit() 'Change this to your binding source ' eg table' 
     Me.TableAdapterManager.UpdateAll(Me.StudentRecords1DataSet) ' chnage this to your database name' 
     MessageBox.Show("The Data Has Been Saved", "Information", MessageBoxButtons.OK) 
    Catch ex As Exception 
     'if there is a problem saving the data, it will show a messagebox with the problem as to why it could'nt save the data' 
     MessageBox.Show(ex.Message) 
    End Try 

    StudentRecords1DataSet.tblStudents.Rows.Add(MyNewRow) 
    StudentRecords1DataSet.tblStudents.AcceptChanges() 

End Sub 

Private Sub txtFirstName_MaskInputRejected(sender As Object, e As MaskInputRejectedEventArgs) 

End Sub 

Private Sub TblStudentsBindingNavigator_RefreshItems(sender As Object, e As EventArgs) Handles TblStudentsBindingNavigator.RefreshItems 

End Sub 

エンドクラス

任意の提案してください?

+0

は、あなたのプロジェクトに、Accessデータベースを追加しましたか? – Werdna

+0

@Werdna ye、アクセスデータベースは 'Add New DataSource'関数 – Donncha

+0

によって追加されます。ちょうどボタンを押してデータベースを保存したいのですか? – Werdna

答えて

1

UPDATe X2ベースの質問更新。

Public Class frmAddStudent 


Private Sub btnBack_Click(sender As Object, e As EventArgs) Handles btnBack.Click 
    Me.Hide() 
    frmUserControls.Show() 
End Sub 

Private Sub frmAddStudent_Load(sender As Object, e As EventArgs) Handles MyBase.Load 
    'TODO: This line of code loads data into the 'StudentRecords1DataSet.tblLecturer' table. You can move, or remove it, as needed. 
    Me.TblLecturerTableAdapter.Fill(Me.StudentRecords1DataSet.tblLecturer) 
    'TODO: This line of code loads data into the 'StudentRecords1DataSet.tblStudents' table. You can move, or remove it, as needed. 
    Me.TblStudentsTableAdapter.Fill(Me.StudentRecords1DataSet.tblStudents) 

End Sub 

Private Async Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click 

    Try 
     Me.Validate() 
     Me.TblStudentsBindingSource.EndEdit() 'Change this to your binding source ' eg table' 
     Me.TableAdapterManager.UpdateAll(Me.StudentRecords1DataSet) ' chnage this to your database name' 
     MessageBox.Show("The Data Has Been Saved", "Information", MessageBoxButtons.OK) 
     Await Task.Delay(100) 
     TblStudentsBindingNavigator_RefreshItems.studentsBindingSource.AddNew() 
    Catch ex As Exception 
     'if there is a problem saving the data, it will show a messagebox with the problem as to why it could'nt save the data' 
     MessageBox.Show(ex.Message) 
    End Try 

End Sub 

Private Sub txtFirstName_MaskInputRejected(sender As Object, e As MaskInputRejectedEventArgs) 

End Sub 

Private Sub TblStudentsBindingNavigator_RefreshItems(sender As Object, e As EventArgs) Handles TblStudentsBindingNavigator.RefreshItems 

End Sub 

エンドクラス、これを試してみてください

+0

私はTry Catchを追加しましたが、私はまだデータの同じ問題を私に保存していませんdb。データが保存されているというメッセージボックスが表示されていますが、行が実際に保存されていません – Donncha

+0

ご質問にすべてのコードを追加できますか?別の場所に問題がなければなりません。データベースが正しくセットアップされていないと、保存されます。 – Werdna

+0

質問 – Donncha

関連する問題