Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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.

  • *Experts*
Posted

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

"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
Posted

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.

Posted

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

  • *Experts*
Posted

Have you tried adding the EndEdit code I mentioned above? I think it will solve your problem.

 

-Nerseus

"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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...