vb.net 如何检查 DataGridView 中是否进行了任何更改

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22436866/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 17:06:23  来源:igfitidea点击:

How to check if any changes were made in DataGridView

xmlvb.netdatagridview

提问by Guardian

In my Windows application there is a DataGridViewform that shows data from xml file.

在我的 Windows 应用程序中,有一个DataGridView表单显示 xml 文件中的数据。

Now I want to check if there any changes in DataGridViewto ask user does he\she wants to save current changes that were done in DataGridViewto the file.

现在我想检查是否有任何更改DataGridView来询问用户他\她是否想将当前所做的更改保存DataGridView到文件中。

回答by ?s??? ????

I would use two events in order to detect any change in the DataGridView. These are CellValueChangedfor detecting changes on fields and CurrentCellDirtyStateChangedfor detecting changes in CheckBox type columns.

我将使用两个事件来检测DataGridView. 这些CellValueChanged用于检测字段的变化和CurrentCellDirtyStateChanged检测 CheckBox 类型列的变化。

Set a boolean flag = truewhen 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.

flag = true当这些事件中的任何一个发生时设置一个布尔值,并在表单关闭时或每当您想要要求用户保存更改时检查此标志的状态。

Example code

示例代码

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

回答by dbasnett

Here is an example using an XML file with 304 timezones. Button1 loads the DGV and Button2 checks to see if anything changed.

这是一个使用具有 304 个时区的 XML 文件的示例。Button1 加载 DGV,Button2 检查是否有任何更改。

Dim ds As New DataSet

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim pathToXMLFile As String = Environment.GetFolderPath( _
                                                   Environment.SpecialFolder.Desktop)
    pathToXMLFile = IO.Path.Combine(pathToXMLFile, "TimeZones.xml")
    dgv1.DataSource = Nothing
    ds.Dispose()
    ds = New DataSet
    ds.ReadXml(pathToXMLFile) 'read the xml
    'after loading all rows are 'changed'
    'set all rows to not changed
    ds.Tables(0).AcceptChanges()
    dgv1.DataSource = ds.Tables(0) 'set datagridviews datasource
End Sub

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    Dim foo As DataTable = ds.Tables(0).GetChanges
    If Not IsNothing(foo) AndAlso foo.Rows.Count > 0 Then
        'there were changes
        Debug.WriteLine(foo.Rows.Count)
        'if you are going to continue to edit
        ds.Tables(0).AcceptChanges()
    End If
End Sub

回答by Charles F Perry

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Me.Validate()
    Me.BindingSource1.EndEdit()
    Me.VisitsTableAdapter.Update(Me.Ds11)
    Button1.Enabled = False
    DataGridView1.NotifyCurrentCellDirty(False)
End Sub

Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
    If Button1.Enabled = True Then
        e.Cancel = True
    End If
End Sub

Private Sub DataGridView1_CurrentCellDirtyStateChanged(sender As Object, e As EventArgs) Handles DataGridView1.CurrentCellDirtyStateChanged
    If DataGridView1.IsCurrentCellDirty = True Then
        Button1.Enabled = True
    End If
End Sub