468 Part IV Database and Web Programming
In this chapter, you’ll focus on the ability of the DataGridView control to display the columns
(fields) and rows (records) of the Faculty2010 .accdb database, the file of structured employee
information that you started working with in Chapter 18. You’ll start by filling a simple
data grid view object with text records from the Access 2007 database, and then you’ll set
a few formatting options. Next you’ll move on to sorting records in data grid view objects
and learning how to add multiple data grid view objects to a form. Finally, you’ll learn
how to adjust DataGridView properties, including the ReadOnly property, which allows or
prevents a user from saving changes back to the original database.
The DataGridView control is connected, or bound, to underlying data access components
through its BindingSource property. This property contains useful information only after
your program has established a connection to a valid data source by using the Data Source
Configuration Wizard and the Data Sources window. (The steps involved in establishing this
connection will be reviewed quickly here but are described in greater detail in Chapter 18;
if you want more information, read the section “Working with an Access Database” in that
chapter .) After a data grid view object is bound to a valid data source, Visual Studio fills, or
populates, the data grid view object automatically by using the Fill method when the form is
loaded into memory.
Establish a connection to a database table
- Start Visual Studio, and then create a new Microsoft Visual Basic Windows Forms
Application project named My DataGridView Sample.
A new project appears in the Integrated Development Environment (IDE). - Click the Add New Data Source command on the Data menu.
The Data Source Configuration Wizard opens in the development environment.
You used this tool in Chapter 18 to link the Faculty2010 .accdb database to your project
and fill the Data Sources window with tables and columns from the database. This time,
you’ll select a broader range of information from the sample Access database.
- Click the Database icon, and then click Next.
- Click the Dataset icon, and then click Next.
The wizard prompts you to build a connection string, but if you completed the
exercises in Chapter 18, the Faculty2010 .accdb database is offered to you automatically,
as shown in the screen shot on the following page: