happycat Posted April 29, 2003 Posted April 29, 2003 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. Quote
*Experts* Nerseus Posted April 29, 2003 *Experts* Posted April 29, 2003 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: Dim row As DataRow For Each row In MyDataSet.Tables("Table1").Rows row.EndEdit() Next If MyDataSet.HasChanges() Then '... End If -Nerseus Quote "I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
happycat Posted April 30, 2003 Author Posted April 30, 2003 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. Quote
*Experts* jfackler Posted April 30, 2003 *Experts* Posted April 30, 2003 Can you show us the entire code block for your closing event? Quote
happycat Posted May 1, 2003 Author Posted May 1, 2003 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 Quote
*Experts* Nerseus Posted May 1, 2003 *Experts* Posted May 1, 2003 Have you tried adding the EndEdit code I mentioned above? I think it will solve your problem. -Nerseus Quote "I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.