Connecting to a Database in Visual Basic

Run the movie again, and make a change and add a new movie. Press save and then check to make sure that the changes are permanent.

Next, we will add the code so that the user will be prompted to save when they exit. The first thing we want to do is set changed to true when the user makes a change to the table:

Private Sub dataGridView1_CellValueChanged(ByVal sender As Object, _
  ByVal e As DataGridViewCellEventArgs) Handles DataGridView1.CellValueChanged
     Changed = True
End Sub 'dataGridView1_CellValueChanged
The user can delete a row, or rows, by selecting the rows and pressing "delete" on the keyboard. This code confirms that they want to delete, and if so, sets changed to true:
Private Sub DataGridView1_UserDeletingRow(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewRowCancelEventArgs) _
  Handles DataGridView1.UserDeletingRow
  Dim Answer As MsgBoxResult
  Answer = MsgBox("Are you sure you want to delete?", MsgBoxStyle.YesNo, "Delete")
  If Answer = MsgBoxResult.No Then
     e.Cancel = True
  Else
     Changed = True
  End If
End Sub 'DataGridView1_UserDeletingRow
Next, we will copy the procedures for MnuExit and AskToSave from the previous lesson:
Private Sub MnuExit_Click(ByVal sender As Object, ByVal e As System.EventArgs) _
    Handles MnuExit.Click
  AskToSave()
End Sub 'mnuExit_Click

NEXT: Data Changes Code