如何检查DataGridView中是否有任何更改 [英] How to check if any changes were made in DataGridView
问题描述
在我的Windows应用程序中有一个 DataGridView
表单,显示xml文件中的数据。
In my Windows application there is a DataGridView
form that shows data from xml file.
现在我想检查 DataGridView
中是否有任何更改要求用户,他是否希望保存在 DataGridView $ c $中完成的当前更改c>到该文件。
Now I want to check if there any changes in DataGridView
to ask user does he\she wants to save current changes that were done in DataGridView
to the file.
推荐答案
我将使用两个事件来检测 DataGridView
。这些是 CellValueChanged
,用于检测字段上的更改和 CurrentCellDirtyStateChanged
,以检测CheckBox类型列中的更改。
I would use two events in order to detect any change in the DataGridView
. These are CellValueChanged
for detecting changes on fields and CurrentCellDirtyStateChanged
for detecting changes in CheckBox type columns.
当任何一个事件发生时,设置一个布尔值 flag = true
,当窗体关闭或者想要的时候检查该标志的状态要求用户保存更改。
Set a boolean flag = true
when either of those events occurs and check the status of this flag when the form is closed or whenever you want to ask the user for saving changes.
示例代码
Dim DGVhasChanged As Boolean
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
DGVhasChanged = False
//Do stuff to populate the DataGridView
End Sub
Private Sub DataGridView1_CellValueChanged(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellValueChanged
DGVhasChanged = True
End Sub
Private Sub DataGridView1_CurrentCellDirtyStateChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DataGridView1.CurrentCellDirtyStateChanged
DGVhasChanged = True
End Sub
//This example check for changes on Form closing but you can check it on any other event (e.g: When a button is clicked)
Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
If DGVhasChanged = True Then
Dim response As MsgBoxResult
response = MsgBox("Do you want to save the changes?", MsgBoxStyle.YesNo)
If response = MsgBoxResult.Yes Then
//Do stuff to save the changes...
DGVhasChanged= False
End If
End If
End Sub
这篇关于如何检查DataGridView中是否有任何更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!