【问题标题】:How to check if any changes were made in DataGridView如何检查 DataGridView 中是否进行了任何更改
【发布时间】:2019-03-07 09:37:48
【问题描述】:

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

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

【问题讨论】:

  • 如果您使用 DataSet 表填充 DataGridView,您可以使用 .GetChanges 方法找出哪些行(如果有)已更改。

标签: xml vb.net datagridview


【解决方案1】:

我将使用两个事件来检测DataGridView 中的任何变化。这些CellValueChanged 用于检测字段的更改,CurrentCellDirtyStateChanged 用于检测 CheckBox 类型列的更改。

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

示例代码

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 中删除/添加新行,奇怪...
  • 并不奇怪。上面的代码没有控制它。只需对 DataGridView.RowsAdded 和 DataGridView.RowsRemoved 事件执行相同操作
【解决方案2】:

这是一个使用具有 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

【讨论】:

    【解决方案3】:
    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
    

    【讨论】:

    • 请考虑在您的代码中添加解释或 cmets,而不仅仅是没有上下文的代码块。
    猜你喜欢
    • 1970-01-01
    • 2011-06-17
    • 1970-01-01
    • 2012-09-25
    • 2011-06-27
    • 2017-04-01
    • 1970-01-01
    • 2020-12-08
    • 1970-01-01
    相关资源
    最近更新 更多