locked
Detect data change RRS feed

  • Question

  • I have a form with TableAdapter, BindingSource, DataNavigator ... and bound controls.

    When moving to a new item (with the DataNavigator buttons) or closing the form - I need a way to detect if the user
    has changed anything to ask if they want to save changes.

    If they answer yes then I will do the appropriate .Update - if they answer no I need to cancel any changes made in the controls.

    Any ideas ?

    Thursday, February 16, 2006 5:16 PM

Answers

  • I have an alternative. If the user already clicked add new then there had been changes made. If the user clicked add new and then closes the form you need to ask him if he wants to save? if he answered yes then save the form, then you run your validation methods to check if the required fields have values, if all is well then save if not then throw an exception.
    Friday, February 17, 2006 12:10 AM

All replies

  • You can check out the HasChanges property on the DataSet: northwindDataSet.HasChanges

    -mark
    Program Manager
    Microsoft
    This post is provided "as-is"

     

    Thursday, February 16, 2006 8:20 PM
  • I don't think that will work - I think .HasChanges will only return True if the .EndEdit has been done - but if the user decides to not save the changes it will be too late...
    Thursday, February 16, 2006 9:51 PM
  • Ok - when you say "moving to a new item" do you mean the new row? If so, you can check the IsCurrentRowDirty and IsCurrentCellDirty. This will basically let you know when the "pencil" icon is showing for a row.

    -mark
    Program Manager
    Microsoft
    This post is provided "as-is"

     

    Thursday, February 16, 2006 10:26 PM
  • First thanks for taking the time to help...

    I am not using the DataGridView ... it's simply a form with bound controls (textboxes, combos) and a DataNavigator. So if the user clicks on the navigator buttons or closes the form - I want to detect if changes were made and ask "Do you want to save?". If yes then I do .EndEdit and ta.Update but if the users responds No I want the cancel the changes (so if I navigate back to the record the original values come back)

     

    Thursday, February 16, 2006 10:54 PM
  • I see. I don't personally know of any way, maybe someone else does.

    -mark
    Program Manager
    Microsoft
    This post is provided "as-is"

     

    Thursday, February 16, 2006 11:21 PM
  • I have an alternative. If the user already clicked add new then there had been changes made. If the user clicked add new and then closes the form you need to ask him if he wants to save? if he answered yes then save the form, then you run your validation methods to check if the required fields have values, if all is well then save if not then throw an exception.
    Friday, February 17, 2006 12:10 AM
  • Ditto on this.  I need the same solution if anyone comes up with one.
    Sunday, March 12, 2006 10:58 PM
  • Here is how I would deal with it. You may also use this code in Form Closing event.


    Private Sub SaveData()

         Me.MyBindingSource.EndEdit()

         Dim dr As DataRow = CType(Me.MyBindingSource.Current, DataRowView).Row

         Dim rs As DataRowState = dr.RowState

         If rs = DataRowState.Added OrElse rs = DataRowState.Modified Then

              If MessageBox.Show("Do you want to save the record?", "", MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.Yes Then

                   Me.MyTableAdapter.Update(dr)

                   MsgBox("The record has been saved")

              End If

         End If

    End Sub


    • Proposed as answer by Steve Jensen Wednesday, April 3, 2013 5:26 AM
    Monday, March 13, 2006 7:56 AM
  • Has enyone come up with a way to tell if the bindingsource contains changes prior to the BindingSource.EndEdit()? In this manner I too,  will be able to ask the user to save changes, and if they don't decide then issure an BindingSource.CancelEdit() instead.

     

    Also the moment I issue an BindingSource.EndEdit(),

    BindSource.Current.Row.RowState is always Modified

    Wednesday, March 15, 2006 1:01 PM
  • This is the best I have come up with so far :

    Private Sub SaveData()

         Me.MyBindingSource.EndEdit()

         Dim dr As DataRow = CType(Me.MyBindingSource.Current, DataRowView).Row

         If dr.Row.RowState <> DataRowState.Unchanged Then

              If MessageBox.Show("Do you want to save the record?", "", MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.Yes Then

                   Me.MyTableAdapter.Update(dr)

                   MsgBox("The record has been saved")

             Else

                   dr.DataView.Table.RejectChanges()

             End If       

         End If

    End Sub

    Sunday, March 19, 2006 6:46 AM
  • I use the following:

    bool changedRows = false;

    foreach (DataRow r in this.DataSet.DataTable.Rows)

    {

    changedRows = r.HasVersion(DataRowVersion.Proposed);

    }

    return changedRows;

    Now if I could figure out how to tell if a new (empty) record is being editied.  I have been working on this one for weeks.

    Tuesday, May 9, 2006 6:14 PM
  • I have been struggling with this, but have come up with this solution.  This function is passed a binding source and the address of the appropriate "Save" function.  It first checks to see if changes have been made to the current record.  If so the user is prompted to save changes (yes/no/cancel).  The function returns true or false which tells the caller whether or not to "proceed" (close the form, load another record, etc).  Hope this helps.  Also make sure to call Validate() before calling this function as this pushes all changes from your bound controls (thus avoiding having to call endedit)

    Delegate Function SaveToDatabase() As Boolean
    Public Function CheckChangesAndProceed(ByRef bindSrc As BindingSource, ByRef SaveFunction As SaveToDatabase) As Boolean
       Dim result As DialogResult
       Dim rowView As DataRowView
       'leave if bindingsource.current is nothing
       If Not bindSrc.Current Is Nothing Then
          rowView = CType(bindSrc.Current, DataRowView)
          If rowView.Row.HasVersion(DataRowVersion.Proposed) Then
             result = MessageBox.Show("Do you want to save changes?", "Save Changes",    MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question)
             Select Case result
                Case DialogResult.Yes
                   'user wants to save changes - call save function
                   If SaveFunction.Invoke = True Then
                      'save function suceeded - proceed
                      Return True
                   Else
                      'save function failed - do not proceed
                      Return False
                   End If
                Case DialogResult.No
                   'user does not want to save changes - proceed
                   bindSrc.CancelEdit()
                   Return True
                Case DialogResult.Cancel
                   'user does not want to save or proceed 
                   Return False
             End Select
          Else
             'no changes to save - proceed
             Return True
          End If
       Else
          'no current record in binding source - proceed
          Return True
       End If
    End Function
    Wednesday, May 10, 2006 4:03 AM