在vb formdesigner中如何从datagridview更新底层表

时间:2017-02-07 18:02:20

标签: vb.net visual-studio-2015 datagridview

我有一个数据网格视图绑定到sql-server数据库。当我在单元格中进行了一些更改后,我可以使用按钮将更改保存到数据库中:

    Private Sub btnUpdate_Click(sender As Object, e As EventArgs) Handles btnUpdate.Click
    Try
        Dim dlgResult As New DialogResult
        dlgResult = MessageBox.Show("Do you want to save the changes you made ?", "Confirmation !", MessageBoxButtons.YesNo)
        If dlgResult = DialogResult.Yes Then
            Try
                Me.tblInvoicesTableAdapter.Update(Me.dsInvoices.tblInvoices)
                MessageBox.Show("Updated successfully !", "Information :", MessageBoxButtons.OK, MessageBoxIcon.Information)
            Catch ex As Exception
                MsgBox(ex.ToString)
            End Try
        Else
            MessageBox.Show("Update canceld !", "Information :", MessageBoxButtons.OK, MessageBoxIcon.Information)

            'Code for undo changes to dgv

            'some testcode - doesn't work
            Me.dgvInvoice.RefreshEdit()
            Me.dgvInvoice.Refresh()
            End If
  Catch ex As Exception
        MessageBox.Show("Error\n" + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
    End Try
End Sub

如何在Else事件的button_Click声明中重置我的更改?

1 个答案:

答案 0 :(得分:0)

使用DataTable作为DataSource,您只需拨打DataTable.RejectChanges()即可。根据MSDN文档,这种方法:

  

回滚自加载后对表格所做的所有更改,或者最后一次调用AcceptChanges