How to check if any changes have been made to the DataGridView

My Windows application has a form DataGridView

that shows data from an XML file.

Now I want to check if there are any changes in DataGridView

, to ask the user if he wants to save the current changes made to DataGridView

a file.

+1


source to share


2 answers


I would use two events to detect any change in DataGridView

. This is CellValueChanged

for detecting changes in fields and CurrentCellDirtyStateChanged

for detecting changes in columns of type CheckBox.

Set to boolean flag = true

when one of these events occurs, and check the state of this flag when the form is closed or when you want to ask the user to save changes.



Sample 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

      

+3


source


Here is an example using an XML file with 304 time zones. Button1 loads the DGV and Button2 checks to see if something has changed.



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

      

0


source







All Articles