Chapter 19 Data Presentation Using the DataGridView Control 479
When database records are sorted, a sorting column, or key, is required—you establish
this key by clicking the heading of the column on which you want to base the sort. The
DataGridView control provides visual identification for the current sort key—a tiny arrow
to the right of the column header. If the sort order is currently an ascending alphabetical
(A–Z) list, the arrow points up. Clicking the column heading will reverse the sort order
to create a descending alphabetical (Z–A) list. The arrow acts like a toggle, so you can
switch back and forth between sorting directions.
- Click the Last Name column several times to see how the sort order can be switched
back and forth. - Click other column headings, such as Department and Faculty Type, to sort the
database based on those keys. - When you’re finished experimenting with the scrolling, resizing, and sorting features of
the DataGridView control, click the Close button on the form to stop the program.
The program closes, and the development environment returns.
Formatting DataGridView Cells
To customize the appearance of your dataset on a form, you can control the look and
orientation of several DataGridView characteristics by setting properties at design time.
For example, you can change the default width of cells in the data grid view, add or remove
column headers, change the data grid view or header background colors, and change
the color of the gridlines. The following exercise guides you through some of these useful
property settings.
Set data grid view properties at design time
- Display the form, and then click the data grid view object (if it is not already selected).
- In the Properties window, click the Columns property, and then click the ellipsis (... )
button in the second column to open the Edit Columns dialog box.
You used this dialog box earlier to remove the ID and Faculty ID columns from the
Faculty table. (This dialog box is also used to set property settings for individual
columns .) Now, you’ll change the default width of the First Name and E-mail Address
columns. - Select the First Name column, and then set the Width property to 60.
A width of 60 (measured in pixels) will provide plenty of room for the names that you
have in the First Name column.
- Select the E-mail Address column, and then set the Width property to 140.
This will provide a little more room for the longer e-mail addresses.