Chapter 19 Data Presentation Using the DataGridView Control 485
Adding a BindingNavigator Control to Create
a Second Navigation Bar on the Form
If you choose to add a second database table to your form, keep in mind that you can
also add a second navigation bar to the form so that your user can use two different
navigation bars at once. To make this work, you need to be using a database that
contains more than one table. (In the Faculty2010 .accdb database, there was only one
table, but it is common for databases to have several tables to choose among .) Follow
these steps to add a second table and navigation bar to your program:
- Use the Data Sources window to create a second data grid view object on your
form, representing a second table in the database. - Customize the table’s fields by setting properties and using the Edit Columns
command. - Double-click the BindingNavigator control on the Data tab of the Toolbox.
Visual Studio adds a binding navigator object named BindingNavigator1 to the
component tray and adds a second navigation bar to the top of your form. You
may need to move the data grid view objects down slightly if the new navigation
bar is covering them. - Change the BindingSource property of the second binding navigator object to
the binding source of the second table. This is made easy by the BindingSource
arrow in the Properties window, which shows the names of the two valid binding
sources in the program, so you can simply pick it from the list. Once a link has
been established between the second navigation bar and the binding source
object representing the second table, your program is ready to run.
One Step Further: Updating the Original Database
As I mentioned earlier, the dataset object in your program is only a representation of the
data in your original database. This is also true of the information stored in the data grid view
objects on your form—if the user makes a change to this data, the change isn’t written back
to the original database unless you have set the data grid view object’s ReadOnly property
to False and the user clicks the Save Data button on the navigation bar. The designers of
ADO .NET and Visual Studio created this relationship to protect the original database and to
allow your users to manipulate data freely in your programs—whether you plan to save the
changes or not.
In the following exercise, you’ll examine the first data grid view object’s ReadOnly property,
which enables or disables changes in the FacultyDataGridView object. You’ll also learn how
to use the Save Data button, which writes changes back to the original database tables
on disk.