RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


The .NET Newbie's Guide to Customizing the DataGridView Control : Page 3

If you're a .NET newbie coming from Access or SQL Server, let .NET do the hard work of creating a connection to foreign data. Then customize the resulting control to add functionality.


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.

Charlotte Foust has been developing relational databases for over 20 years, and has focused on Microsoft Access since its initial release in 1992. She has designed and consulted on databases for a number of different industries. Charlotte has been an MVP in Woody's Lounge for many years, and presently serves as a moderator for the MS Access board at that site.
Email AuthorEmail Author
Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date