
400
LESSON 35 Programming Databases, Part 1
MessageBoxIcon.Question);
if (result == DialogResult.Cancel)
{
// Cancel the close.
e.Cancel = true;
}
else if (result == DialogResult.Yes)
{
// Save the changes.
peopleNamesTableAdapter.Update(peopleDataSet);
// Make sure the save worked.
// If we still have unsaved changes, cancel.
e.Cancel = (this.peopleDataSet.HasChanges());
}
// Else the user doesn’t want to save
// the changes so just keep going.
}
}
If the dataset has unsaved changes, the code asks the user whether it should save the changes. If the
user clicks Cancel, the code sets
e.Cancel to true so the program doesn’t close the form.
If the user clicks Yes, the code call’s the table adapter’s
Update method to save the dataset’s changes
back to the database.
If the user clicks No, the code just continues and lets the form close without saving the changes.
DISPLAYING RECORDS ONE ROW AT A TIME
Instead of displaying a table’s records in a grid, you can display
the data one record at a time as shown in Figure 35-7.
With this kind of interface, you can click the navigation but-
tons on the
BindingNavigator to move through the records.
You can use the display controls (
TextBoxes in Figure 35-7) to
change a record’s values.
To build this interface, first create a data source as before. Then,
instead of dragging a table from the Data Sources window onto
the form, drag individual fields onto ...