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 grids 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 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 InstructorsDataViewGrid object. You'll also learn how to use the Save Data button, which writes changes back to the original database tables on disk.
Enable updates to the database
Click the first data grid view object on the form (InstructorsDataViewGrid), and then highlight the Properties window.
Scroll to the ReadOnly property, and examine its property setting.
If the ReadOnly property is set to False, the user is free to make changes to the information in grid cells. If you want to allow your users to modify the information and write it back to the database your program is connected to, you should keep this default setting. If you want to disable editing, you should set the ReadOnly property to True.
You'll keep the default setting of False in this case—you want to test updating the underlying Students.mdb database.
TIP
The complete DataGridView Sample program is located in the c:\vb05sbs\chap19\datagridview sample folder.
Click the Start Debugging button to test the first grid's Read Only property.
The two grids appear with data from the Instructors table and the Classes table of the Students.mdb database.
In the first grid, click the cell containing the phone number for Kim Halvorson, type 1234567890, and then press Enter.
As you make the change, a tiny pencil icon appears in the row header to the left, indicating that a change is being made. Your screen looks similar to this:
When you press Enter or click a different cell in the grid, the change is stored in the StudentsDataSet dataset.
Click the Save Data button on the navigation bar.
Visual Studio uses the Update method in the grid's table adapter object to write the changed dataset to the underlying database. The program statement used to accomplish this save operation in the bindingNavigatorSaveItem_Click event procedure looks like this:
Me.InstructorsTableAdapter.Update(Me.StudentsDataSet.Instructors)
After the save operation, the Instructors table is permanently updated.
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 Instructors table in the Students.mdb 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. As you can see, the row in the Instructors table containing the name Kim Halvorson has been updated with the changed telephone number. 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 girds, set ReadOnly to True now, and see what happens when you try to modify the database. (You won't be able to 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 grid with property settings and how to write table updates from the grid back to the original database. As you can begin to see, database programming with ADO.NET and Visual Studio is straightforward but also somewhat involved. There are many tools, components, and programming techniques related to viewing, manipulating, and updating database records, and we haven't even begun to talk seriously about important issues such as security and what happens when you work with large databases that are being used by many users at the same time. Although you've been able to accomplish a lot with little or no program code, there is still much to learn if you plan to make extensive use of databases within Visual Basic applications. For a list of books I recommend for you to continue your studies, see Appendix A, “Where to Go for More Information.”
Data Access in a Web Forms Environment
The data access techniques discussed in Chapter 18 and this chapter were designed for use in the Windows Forms Designer—the Visual Studio environment you've used to build most of the programs in this book. However, you can also use ADO.NET programming techniques in a Web Forms environment, which allows you to share data resources over the Internet and datacentric applications that are accessible through a Web browser such as Internet Explorer. I'll show you how to do this near the end of the next chapter, and you'll learn how to use a few new tools there too, including the GridView control, a version of the DataGridView control designed for displaying database tables on Web sites.