Do you want to save your changes? (VB)

happycat

Newcomer
Joined
Mar 4, 2003
Messages
11
Do you want to save your changes?

This is a simple question. I've searched for the answer, but no luck.

I want to check a datagrid for changes when a user clicks the 'X' to close the form. If the datagrid has changes, then I want to display a message "Would you like to save your changes . . "

I put my code in the form_closing event.

If MyDataSet.HasChanges() Then . . . .

In this event, the MyDataSet.HasChanges() returns false.

What event should I use?

Thanks for the help.
 
Your code should be working fine. Try finding the currently edited row and calling EndEdit on it, to make sure that the row the user is currently on is updated in the DataSet (a requirement for HasChanges to report it).

If you don't know how to get the currently edited row you can loop through all rows of all tables and call EndEdit on every row. If your table is named "Table1", try something like this:
Visual Basic:
        Dim row As DataRow
        For Each row In MyDataSet.Tables("Table1").Rows
            row.EndEdit()
        Next
        If MyDataSet.HasChanges() Then
            '...
        End If

-Nerseus
 
Thanks for the info.
I haven't gotten it to work yet, but I'll keep trying. I think it's strange that when I move the code to a button, I receive the "Do you want to save your changes" message. I click the button right before I click the close button on the form, and the HaveChanges() evaluates to True, but when I click the close button it evaluates to false.
 
This is the same code I copied into a button. I works when I click the button, but not when I close the form (by clicking the 'X' on the form). Do I have the correct event?

Private Sub Form1_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing

If MyDataSet.HasChanges() Then
Dim msg As String
Dim title As String
Dim style As MsgBoxStyle
Dim response As MsgBoxResult
msg = "Do you want to save your changes?"
style = MsgBoxStyle.DefaultButton2 Or _
MsgBoxStyle.Critical Or MsgBoxStyle.YesNo
title = "Changed Data Has Not Been Saved"
response = MsgBox(msg, style, title)
If response = MsgBoxResult.Yes Then ' User chose Yes.
OleDbDataAdapter2.Update (MyDataSet)
End If
Else
' NO changed rows were detected
End If

End Sub
 
Back
Top