// Make sure the save worked.
// If we still have unsaved changes, cancel.
e.Cancel = (this.contactsDataSet.HasChanges());
}
// Else the user doesn’t want to save
// the changes so just keep going.
}
}
Please select Lesson 35 on the DVD to view the video that accompanies this lesson.
EXERCISES
1. Make a program similar to the one you built for the Try It except make it display one record at
a time instead of using a grid. Anchor the
TextBoxes so they widen if the form widens. Don’t
forget to enable the Add New and Delete buttons, and to add the
FormClosing event handler.
2. Copy the program you built for this lesson’s Try It. That program’s grid lets the user navigate
through the records, add records, and delete records, so you don’t really need all of those
buttons on the
BindingNavigator. Select the BindingNavigator. In the Properties window,
click the Items property and click the ellipsis to the right. Set the
Visible property to false
for every item except the position, count, and save items.
3. Copy the program you built for Exercise 1. Add a MenuStrip with a Data menu that has
items First, Prev, Next, Last, Add New, Delete, and Save. Set the
Visible property on the
corresponding
BindingNavigator buttons to false.
To make the menu items work, use the
BindingSource’s CurrencyManager. That
object’s properties and methods let you manipulate the current record (hence the
name CurrencyManager). For example, the following code sets the current position
to the first record:
this.contactsBindingSource.CurrencyManager.Position = 0;
Add or subtract one from Position to move to the next or previous record. Set Position to
the
CurrencyManager’s List.Count - 1 value to move to the end of the list.
Use the
RemoveAt method to delete the current record.
Finally, to save changes to the data, write a
SaveChanges method similar to this one:
private void SaveChanges()
{
this.Validate();
this.contactsBindingSource.EndEdit();
this.tableAdapterManager.UpdateAll(this.contactsDataSet);
}
596906c35.indd 403 4/7/10 12:34:53 PM