【问题标题】:Changes To Data Set Not Saving对数据集的更改未保存
【发布时间】:2016-07-13 01:40:51
【问题描述】:

当我向我的数据集添加新行时,它在 datagridview 中的特定表单上可见,但是当我切换到具有相同数据绑定 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

结束类

有什么建议吗?

【问题讨论】:

  • 你的access数据库添加到你的项目了吗?
  • @Werdna ye,访问db是通过Add New DataSource函数添加的
  • 你只想要一个按钮来保存数据库是吗?

标签: vb.net datagridview dataset


【解决方案1】:

根据您的问题更新更新 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

结束类

【讨论】:

  • 我已经添加了 Try Catch,但是我仍然遇到同样的问题,即数据没有保存到我的数据库中。显示数据已保存的消息框,但实际上并未保存行
  • 能否将所有代码添加到您的问题中?一定是其他地方有问题,是的,如果数据库没有正确设置,它将保存完成。
  • 您是否将网格从“数据源”拖过?
  • 另外你创建新行的方式也不正确,你应该尝试Customers_InformationBindingSource.AddNew()之类的方法,它会自动为access数据库添加一个新行。
  • 是的,网格被拖过,文本框也是如此
猜你喜欢
  • 2015-01-18
  • 2016-11-08
  • 1970-01-01
  • 2013-10-28
  • 2022-01-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-01-31
相关资源
最近更新 更多