Chapter 19 Data Presentation Using the DataGridView Control 487
When you press Enter or click a different cell in the data grid view object, the change is
stored in the Faculty2010DataSet dataset.
- Click the Save Data button on the navigation bar.
Visual Studio uses the UpdateAll method in the data grid view’s table adapter object
to write the changed dataset to the underlying database. The program statement used
to accomplish this save operation in the FacultyBindingNavigatorSaveItem_Click event
procedure looks like this:
Me.TableAdapterManager.UpdateAll(Me.Faculty2010DataSet)
TableAdapterManager is the component in Visual Studio 2010 that allows you to control
one or more database tables in a program. The UpdateAll method saves changes in all
open tables in a program, which means that it saves changes not only in the Faculty
table, but any other table that you have open. You do not have to accept the default
saving behavior here. If you would like to save only the changes in the Faculty table
when your user clicks the Save Data button, replace the statement above with the
following line of code:
Me.FacultyTableAdapter.Update(Me.Faculty2010DataSet.Faculty)
If you use the Update method for a named table adapter object, then just that table
adapter’s associated data will be saved. (And remember, you can also control user edits
within tables by using the ReadOnly property .)
- Click the Close button to end the program.
The program closes and the Visual Studio IDE returns. Now, you’ll run the program
again to see whether the Faculty table in the Faculty2010 .accdb database has indeed
been modified. (When you restart the program, it will load data from the database file .)
- Click the Start Debugging button.
After a moment, the data grid view objects are loaded with data. You will see that the
row in the Faculty table containing the name Wilson Pais has been updated with the
change to Assistant Professor. The program works!
- Click the Close button to end the program.
If you want to continue experimenting with the ReadOnly property for one or both of the
data grid view objects, set ReadOnly to True now, and see what happens when you try to
modify the database. (You won’t be able to make edits or save any changes .) You might
also experiment with adding new rows of data to the database by using the built-in editing
features and toolbar buttons associated with the DataGridView and BindingNavigator
controls. (Before you add new rows, set the ReadOnly property back to False .)
Now take stock of your accomplishments. You’ve learned to display multiple tables and
records by using the DataGridView and BindingNavigator controls, and you’ve learned
how to customize the data grid view objects with property settings and how to write