我有一个数据网格视图绑定到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
声明中重置我的更改?
答案 0 :(得分:0)
使用DataTable
作为DataSource
,您只需拨打DataTable.RejectChanges()
即可。根据MSDN文档,这种方法:
回滚自加载后对表格所做的所有更改,或者最后一次调用AcceptChanges。