Enhancement Three: Display Edit Row
Currently, the example form displays 91 records. As is, users wanting to enter a new record must scroll through all the records to access the edit row. As an enhancement, you could program the form to display the edit row automatically. Add the following statement to the form's
Load event, as shown in
Figure 9:
Me.CustomersDataGridView.FirstDisplayedScrollingRowIndex = CustomersDataGridView.Rows.Count - 1
 | |
Figure 9. Added Statement in the Form's Load Event: Force the form to show the edit row when loaded. |
Press F5 to run the form. As you can see in
Figure 10, the form displays a few records and the edit row. The statement subtracts one from the row count, which prevents an exception because the edit row doesn't actually have an index until you click in it, even though it affects the row count. Contrary to what you might think, this process just makes sure that the last row is visible. All of the records are still available through scrolling. The number of existing records the form displays depends on how many it can accommodate (by size). The statement doesn't have anything to do with the number of existing records the form displays.
By disabling the edit row, you could just as easily turn this form into a simple browsing form that doesn't accept new records. Simply add the following statement to the form's Load event:
DataGridView1.AllowUserToAddRows = False
 | |
Figure 10. Form Displaying Records and Edit Row: Now when you launch the form, it displays the edit row. |
Set the
AllowUserToAddRows property to
True to enable the edit row.
Customized Native Controls for Ease and Flexibility
It took very little time to customize the example form:
- Alternating row color required a simple property change.
- Inhibiting the built-in sort required one row of code.
- Displaying the edit row required one row of code.
If you're coming from Access or SQL Server, you can certainly appreciate the elegance and ease with which you can create and implement a form for browsing or editing data.